Skip to content

Commit 8b27831

Browse files
hartkoppgregkh
authored andcommitted
can: bcm: fix CAN frame rx/tx statistics
commit e6c24ba upstream. KCSAN detected a data race within the bcm_rx_handler() when two CAN frames have been simultaneously received and processed in a single rx op by two different CPUs. Use atomic operations with (signed) long data types to access the statistics in the hot path to fix the KCSAN complaint. Additionally simplify the update and check of statistics overflow by using the atomic operations in separate bcm_update_[rx|tx]_stats() functions. The rx variant runs under bcm_rx_update_lock to prevent races when resetting the two rx counters; the tx variant runs under bcm_tx_lock and only needs to guard its own counter's overflow. As the rx path resets its values already at LONG_MAX / 100, there is no conflict between the two locking domains (bcm_rx_update_lock vs. bcm_tx_lock) even for ops that use both paths. The rx statistics update and the frames_filtered update in bcm_rx_changed() were previously performed in two separate bcm_rx_update_lock sections. For an rx op subscribed on all interfaces (ifindex == 0), bcm_rx_handler() can run concurrently on different CPUs, so a counter reset by one CPU between these two sections could leave frames_filtered larger than frames_abs on another CPU, producing a bogus (even negative) reduction percentage in procfs. Update the statistics in the same critical section as bcm_rx_changed() to close this gap, which also removes the now unneeded extra lock/unlock pair around the traffic_flags calculation. Fixes: ffd980f ("[CAN]: Add broadcast manager (bcm) protocol") Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Link: https://patch.msgid.link/20260714-bcm_fixes-v15-4-562f7e3e42da@hartkopp.net Cc: stable@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a7eb6db commit 8b27831

1 file changed

Lines changed: 45 additions & 22 deletions

File tree

net/can/bcm.c

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct bcm_op {
109109
int ifindex;
110110
canid_t can_id;
111111
u32 flags;
112-
unsigned long frames_abs, frames_filtered;
112+
atomic_long_t frames_abs, frames_filtered;
113113
struct bcm_timeval ival1, ival2;
114114
struct hrtimer timer, thrtimer;
115115
ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
@@ -216,10 +216,13 @@ static int bcm_proc_show(struct seq_file *m, void *v)
216216

217217
list_for_each_entry_rcu(op, &bo->rx_ops, list) {
218218

219-
unsigned long reduction;
219+
long reduction, frames_filtered, frames_abs;
220+
221+
frames_filtered = atomic_long_read(&op->frames_filtered);
222+
frames_abs = atomic_long_read(&op->frames_abs);
220223

221224
/* print only active entries & prevent division by zero */
222-
if (!op->frames_abs)
225+
if (!frames_abs)
223226
continue;
224227

225228
seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
@@ -241,9 +244,9 @@ static int bcm_proc_show(struct seq_file *m, void *v)
241244
(long long)ktime_to_us(op->kt_ival2));
242245

243246
seq_printf(m, "# recv %ld (%ld) => reduction: ",
244-
op->frames_filtered, op->frames_abs);
247+
frames_filtered, frames_abs);
245248

246-
reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
249+
reduction = 100 - (frames_filtered * 100) / frames_abs;
247250

248251
seq_printf(m, "%s%ld%%\n",
249252
(reduction == 100) ? "near " : "", reduction);
@@ -267,7 +270,8 @@ static int bcm_proc_show(struct seq_file *m, void *v)
267270
seq_printf(m, "t2=%lld ",
268271
(long long)ktime_to_us(op->kt_ival2));
269272

270-
seq_printf(m, "# sent %ld\n", op->frames_abs);
273+
seq_printf(m, "# sent %ld\n",
274+
atomic_long_read(&op->frames_abs));
271275
}
272276
seq_putc(m, '\n');
273277

@@ -277,6 +281,24 @@ static int bcm_proc_show(struct seq_file *m, void *v)
277281
}
278282
#endif /* CONFIG_PROC_FS */
279283

284+
static void bcm_update_rx_stats(struct bcm_op *op)
285+
{
286+
/* prevent overflow of the reduction% calculation in bcm_proc_show() */
287+
if (atomic_long_inc_return(&op->frames_abs) > LONG_MAX / 100) {
288+
atomic_long_set(&op->frames_filtered, 0);
289+
atomic_long_set(&op->frames_abs, 0);
290+
}
291+
}
292+
293+
static void bcm_update_tx_stats(struct bcm_op *op)
294+
{
295+
/* tx_op has no reduction% calculation - use the full range and
296+
* just keep the displayed counter non-negative on overflow
297+
*/
298+
if (atomic_long_inc_return(&op->frames_abs) == LONG_MAX)
299+
atomic_long_set(&op->frames_abs, 0);
300+
}
301+
280302
/*
281303
* bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
282304
* of the given bcm tx op
@@ -328,7 +350,7 @@ static void bcm_can_tx(struct bcm_op *op, struct canfd_frame *cf)
328350
spin_lock_bh(&op->bcm_tx_lock);
329351

330352
if (!err)
331-
op->frames_abs++;
353+
bcm_update_tx_stats(op);
332354

333355
/* only advance the cyclic sequence if nothing reset currframe while
334356
* we were sending - a concurrent TX_RESET_MULTI_IDX means this
@@ -473,12 +495,9 @@ static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
473495
{
474496
struct bcm_msg_head head;
475497

476-
/* update statistics */
477-
op->frames_filtered++;
478-
479-
/* prevent statistics overflow */
480-
if (op->frames_filtered > ULONG_MAX/100)
481-
op->frames_filtered = op->frames_abs = 0;
498+
/* update statistics (frames_filtered <= frames_abs) */
499+
if (atomic_long_read(&op->frames_abs))
500+
atomic_long_inc(&op->frames_filtered);
482501

483502
/* this element is not throttled anymore */
484503
data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
@@ -718,25 +737,29 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
718737
op->rx_stamp = skb->tstamp;
719738
/* save originator for recvfrom() */
720739
op->rx_ifindex = skb->dev->ifindex;
721-
/* update statistics */
722-
op->frames_abs++;
723740

724-
/* snapshot the flag under lock: op->flags/op->frames may be updated
725-
* concurrently by bcm_rx_setup().
726-
*/
741+
/* op->flags/op->frames may be updated concurrently by bcm_rx_setup() */
727742
spin_lock_bh(&op->bcm_rx_update_lock);
743+
728744
rtr_frame = op->flags & RX_RTR_FRAME;
729-
if (rtr_frame)
745+
if (rtr_frame) {
746+
bcm_update_rx_stats(op);
747+
/* snapshot RTR content under lock */
730748
memcpy(&rtrframe, op->frames, op->cfsiz);
731-
spin_unlock_bh(&op->bcm_rx_update_lock);
749+
spin_unlock_bh(&op->bcm_rx_update_lock);
732750

733-
if (rtr_frame) {
734751
/* send reply for RTR-request (placed in op->frames[0]) */
735752
bcm_can_tx(op, &rtrframe);
736753
return;
737754
}
738755

739-
spin_lock_bh(&op->bcm_rx_update_lock);
756+
/* update statistics in the same critical section as bcm_rx_changed()
757+
* below: frames_filtered must never be checked/incremented against a
758+
* frames_abs snapshot from a concurrent bcm_rx_handler() call on
759+
* another CPU for the same (wildcard) op, or frames_filtered can end
760+
* up larger than frames_abs.
761+
*/
762+
bcm_update_rx_stats(op);
740763

741764
if (op->flags & RX_FILTER_ID) {
742765
/* the easiest case */

0 commit comments

Comments
 (0)