Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Marc Kleine-Budde" <mkl@pengutronix.de>
Cc: linux-can@vger.kernel.org,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH net-next 05/12] can: proc: reset pkg_stats atomics individually
Date: Mon, 19 Oct 2026 16:50:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-177945@kernel.org> (raw)
In-Reply-To: <20261018164417.1540449-6-mkl@pengutronix.de>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] can: proc: pre-reset local snapshots overwrite zeroed stats in can_stat_update()
--

commit 569f80e5a195e52b5ac88d8f5cdb36714d69c5db
Author: Runyu Xiao <runyu.xiao@seu.edu.cn>

can: proc: reset pkg_stats atomics individually

This patch replaces the whole-struct memset() in can_init_stats() with
can_reset_pkg_stats() to reset atomic_long_t counters via atomic_long_set()
and scalar fields individually. It also uses atomic_long_xchg() in
can_stat_update() to fetch and clear the *_delta counters atomically.

Link: https://patch.msgid.link/20261016110905.422684-1-socketcan@hartkopp.net

> diff --git a/net/can/proc.c b/net/can/proc.c
> index de4d05ae34597..70104f13077e0 100644
> --- a/net/can/proc.c
> +++ b/net/can/proc.c
> @@ -76,16 +76,39 @@ static const char rx_list_name[][8] = {
>   * af_can statistics stuff
>   */
>  
> +static void can_reset_pkg_stats(struct can_pkg_stats *pkg_stats)
> +{
> +	atomic_long_set(&pkg_stats->rx_frames, 0);
> +	atomic_long_set(&pkg_stats->tx_frames, 0);
> +	atomic_long_set(&pkg_stats->matches, 0);
> +
> +	pkg_stats->total_rx_rate = 0;
> +	pkg_stats->total_tx_rate = 0;
> +	pkg_stats->total_rx_match_ratio = 0;
> +
> +	pkg_stats->current_rx_rate = 0;
> +	pkg_stats->current_tx_rate = 0;
> +	pkg_stats->current_rx_match_ratio = 0;
> +
> +	pkg_stats->max_rx_rate = 0;
> +	pkg_stats->max_tx_rate = 0;
> +	pkg_stats->max_rx_match_ratio = 0;
> +
> +	atomic_long_set(&pkg_stats->rx_frames_delta, 0);
> +	atomic_long_set(&pkg_stats->tx_frames_delta, 0);
> +	atomic_long_set(&pkg_stats->matches_delta, 0);
> +}
> +
>  static void can_init_stats(struct net *net)
>  {
>  	struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
>  	struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
>  	/*
> -	 * This memset function is called from a timer context (when
> +	 * This stats reset is called from a timer context (when
>  	 * can_stattimer is active which is the default) OR in a process
>  	 * context (reading the proc_fs when can_stattimer is disabled).
>  	 */
> -	memset(pkg_stats, 0, sizeof(struct can_pkg_stats));
> +	can_reset_pkg_stats(pkg_stats);
>  	pkg_stats->jiffies_init = jiffies;
>  
>  	rcv_lists_stats->stats_reset++;
> @@ -121,9 +144,10 @@ void can_stat_update(struct timer_list *t)
>  	long rx_frames = atomic_long_read(&pkg_stats->rx_frames);
>  	long tx_frames = atomic_long_read(&pkg_stats->tx_frames);
>  	long matches = atomic_long_read(&pkg_stats->matches);
> -	long rx_frames_delta = atomic_long_read(&pkg_stats->rx_frames_delta);
> -	long tx_frames_delta = atomic_long_read(&pkg_stats->tx_frames_delta);
> -	long matches_delta = atomic_long_read(&pkg_stats->matches_delta);
> +	/* fetch and clear values for 'current rate' calculation */
> +	long rx_frames_delta = atomic_long_xchg(&pkg_stats->rx_frames_delta, 0);
> +	long tx_frames_delta = atomic_long_xchg(&pkg_stats->tx_frames_delta, 0);
> +	long matches_delta = atomic_long_xchg(&pkg_stats->matches_delta, 0);
>  
>  	/* restart counting in timer context on user request */
>  	if (user_reset)

[Severity: Medium]
In net/can/proc.c:can_stat_update(), the counters and deltas are read into
local variables before the reset checks:

	if (user_reset)
		can_init_stats(net);

	/* restart counting on jiffies overflow */
	if (j < pkg_stats->jiffies_init)
		can_init_stats(net);

	/* prevent overflow in calc_rate() */
	if (rx_frames > (LONG_MAX / HZ))
		can_init_stats(net);
	...

When a user reads /proc/net/can/reset_stats (setting user_reset = 1) or an
overflow condition triggers, can_init_stats(net) -> can_reset_pkg_stats()
zeroes all the counters and scalar fields in pkg_stats, and updates
pkg_stats->jiffies_init = jiffies.

However, because can_stat_update() does not return early or clear those
local snapshot variables after calling can_init_stats(net), it proceeds to
recompute the statistics using the stale pre-reset values:

	/* calc total values */
	if (rx_frames)
		pkg_stats->total_rx_match_ratio = (matches * 100) / rx_frames;

	pkg_stats->total_tx_rate = calc_rate(pkg_stats->jiffies_init, j,
					    tx_frames);
	pkg_stats->total_rx_rate = calc_rate(pkg_stats->jiffies_init, j,
					    rx_frames);

	/* calc current values */
	if (rx_frames_delta)
		pkg_stats->current_rx_match_ratio =
			(matches_delta * 100) /	rx_frames_delta;

	pkg_stats->current_tx_rate = calc_rate(0, HZ, tx_frames_delta);
	pkg_stats->current_rx_rate = calc_rate(0, HZ, rx_frames_delta);

	/* check / update maximum values */
	if (pkg_stats->max_tx_rate < pkg_stats->current_tx_rate)
		pkg_stats->max_tx_rate = pkg_stats->current_tx_rate;
	...

Doesn't this immediately overwrite pkg_stats->total_rx_match_ratio,
current_rx_match_ratio, current_tx_rate, current_rx_rate, max_tx_rate,
max_rx_rate, and max_rx_match_ratio with pre-reset values right after
can_reset_pkg_stats() cleared them (with max_* and match ratios persisting
across subsequent timer ticks)?

Additionally, if jiffies advances between the initial snapshot of j at the
start of can_stat_update() and pkg_stats->jiffies_init = jiffies in
can_init_stats(), the subsequent check (j < pkg_stats->jiffies_init) will
evaluate to true, calling can_init_stats(net) a second time and passing
oldjif > newjif into calc_rate(). And if an overflow check on rx_frames,
tx_frames, or matches triggered the reset, the rate and ratio calculations
still run with the overflowing local values.

Should can_stat_update() re-arm the timer and return early when a reset
occurs, or perform the reset checks before snapshotting the counters?

[ ... ]

>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261018164417.1540449-1-mkl@pengutronix.de?part=5

  reply	other threads:[~2026-10-19 16:50 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 16:41 [PATCH net-next 0/12] pull-request: can-next 2026-10-18 Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 01/12] can: dev: can_dropped_invalid_skb: drop CAN XL frames on non-CAN XL devices Marc Kleine-Budde
2026-10-18 16:54   ` netdev-bot+sinfo
2026-10-18 16:41 ` [PATCH net-next 02/12] can: raw: remove redundant NULL check before netdev_hold() Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 03/12] can: replace ARPHRD_CAN type checks with can_get_ml_priv() Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 04/12] can: isotp: fix filter removal race between release and netns change Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 05/12] can: proc: reset pkg_stats atomics individually Marc Kleine-Budde
2026-10-19 16:50   ` sashiko-bot [this message]
2026-10-19 16:54     ` Oliver Hartkopp
2026-10-18 16:41 ` [PATCH net-next 06/12] can: bcm: fix filter removal race between release and netns change Marc Kleine-Budde
2026-10-18 18:37   ` Oliver Hartkopp
2026-10-19  8:30     ` Oliver Hartkopp
2026-10-19  9:18     ` Marc Kleine-Budde
2026-10-19 18:07   ` netdev-bot+sashiko
2026-10-19 19:20     ` Oliver Hartkopp
2026-10-18 16:41 ` [PATCH net-next 07/12] can: proc: remove pointers from CAN specific proc output Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 08/12] can: raw: take rtnl_lock() before leaving the notifier list Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 09/12] can: isotp: check the frame type, not just the length Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 10/12] can: skb: make echo skb freeing safe in any IRQ context Marc Kleine-Budde
2026-10-19 18:07   ` netdev-bot+sashiko
2026-10-18 16:41 ` [PATCH net-next 11/12] can: skb: make CAN skb allocation failure paths IRQ-safe Marc Kleine-Budde
2026-10-18 16:41 ` [PATCH net-next 12/12] can: dev: can_put_echo_skb(): free skb on invalid echo index Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-177945@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox