🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #5024

[PATCH net-next 07/11] can: proc: reset pkg_stats atomics individually

#5024
From: Marc Kleine-Budde
Date: Mon, 19 Oct 2026 17:01
122 lines
15909 bytes
From: Runyu Xiao <runyu.xiao@seu.edu.cn>

Commit 80b5f90158d1 ("can: statistics: use atomic access in hot path")
converted several members of struct can_pkg_stats to atomic_long_t and
updated the hot TX/RX and procfs read paths to use atomic_long_*()
helpers. However, can_init_stats() still clears the whole struct with
memset().

can_init_stats() is reached from can_stat_update() in timer context and
also from the procfs reset path. Those paths can run while the TX/RX hot
paths are concurrently updating rx_frames, tx_frames, matches and their
*_delta counters. Hitting the whole-struct memset() in that window
performs plain writes to fields that otherwise follow an atomic_long_t
access contract, which can lose or mix live statistics updates.

This issue was found by source-level API-misuse analysis looking for
whole-object resets left behind after atomic_long_t conversions, then
manually audited on Linux v6.18.21.

Replace the whole-struct memset() with a helper that resets the
atomic_long_t counters via atomic_long_set() and clears the derived
scalar statistics explicitly. This preserves the existing reset
semantics for scalar fields while restoring atomic access discipline for
the live counters.

Build-tested by compiling net/can/proc.o on x86_64 netdev/main.
Runtime-tested with a QEMU + vcan setup on Linux v6.18.21 by driving
concurrent traffic and reset_stats reads, which reproduced inconsistent
exported statistics before the fix.

[Hartkopp] can_stat_update() also read the *_delta counters at the
beginning and cleared them with atomic_long_set() at the end. Increments
from the hot path in between were lost for both the current and the next
interval. Use atomic_long_xchg() to fetch and clear the *_delta counters
in one atomic step instead.

Fixes: 80b5f90158d1 ("can: statistics: use atomic access in hot path")
Cc: stable@vger.kernel.org
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261019152741.54854-2-socketcan@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/proc.c | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/net/can/proc.c b/net/can/proc.c
index de4d05ae3459..70104f13077e 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)
@@ -172,11 +196,6 @@ void can_stat_update(struct timer_list *t)
 	if (pkg_stats->max_rx_match_ratio < pkg_stats->current_rx_match_ratio)
 		pkg_stats->max_rx_match_ratio = pkg_stats->current_rx_match_ratio;
 
-	/* clear values for 'current rate' calculation */
-	atomic_long_set(&pkg_stats->tx_frames_delta, 0);
-	atomic_long_set(&pkg_stats->rx_frames_delta, 0);
-	atomic_long_set(&pkg_stats->matches_delta, 0);
-
 	/* restart timer (one second) */
 	mod_timer(&net->can.stattimer, round_jiffies(jiffies + HZ));
 }
-- 
2.53.0
Technical Details
Message-ID: <20261019170834.1727150-8-mkl@pengutronix.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-348733-linux-kernel-netdev=dewy.ru
References: <20261019170834.1727150-1-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-348733-linux-kernel-netdev=dewy.ru
From: mkl@pengutronix.de (Marc Kleine-Budde)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next 07/11] can: proc: reset pkg_stats atomics individually
Date: Mon, 19 Oct 2026 19:01:12 +0200
Organization: Usenet Network
Message-ID: <20261019170834.1727150-8-mkl@pengutronix.de>
References: <20261019170834.1727150-1-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3666543"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:KolfNdE5V7GDCNnTOi8NhsOb/dQ= sha256:e2puZQJwnLy2h0M+ZpyV6YiA0SVA3MSV1hMBgnIxl/o=
	sha1:3ukAoiFNQjxLXSvYXlmNxNgRMN8= sha256:8B22/xI8cnR2TQGLSPstFvJoxnmtz8Th+4sXgB+4LWg=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id BE599110A7E; Mon, 19 Oct 2026 20:32:49 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.15
X-Spam-Status: No, score=-3.15 tagged_above=-9999 required=6.31
	tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
	HEADER_FROM_DIFFERENT_DOMAINS=0.25, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=pengutronix.de
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 4hM7JWC-i-gU;
	Mon, 19 Oct 2026 20:32:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-348733-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id 8325B110A7C; Mon, 19 Oct 2026 20:32:38 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id BA10D610334; Mon, 19 Oct 2026 17:09:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792429734;
	b=PolEigcrzpuK4AteKNWxOKn+R4pLXfxU6yjPYE99E4egWEMVO8aHp8PaT/jZrT0NT5tz
	 9UfSDltvnUWEbxLxAKlSoJ/sefVyMtpCJ0N844zv9dLxc6yjvTz3gvNu6YJKVzvMKF5F4
	 I/LsfHxeTNBRJO5mzJU8Tr40+qJxql7iMYBPytM2KWgCZJvblC+cV7tX916CtSZxqq+ox
	 K5bxka9rjeQDbiFYkyl2MS7I1HVUwEek4YNs9vclqejeTCddeDmI67QZW5zSc6PhWesrN
	 QV7BZIfOXhDxcdRDskKGbZgzoVJKjUKhDVq01UUnOrr0wDlFp8ka742QQRWtn/wwIgQ==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792429734;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=b/+LLk0g1xg6DsRCjMqT66ZWRjvKGdYEhmiOljPoLAI=;
	b=wL2qXBk8dlfcciSECbHmklpMn4WOr5z5f8Ny+Z6gjFmCyzRyge+zRJxeJKAoEJFXoXnu
	 LPPoEOeHMvCDaYsIdUuGIp7zhpnTwwu7/htU910mPI9ArnZugSS9+t6FnzEPQZ51uoQFk
	 E5qEyUSV6OMPXxJtZ1yIF+cGcYtdy+qGDdW4YuHxfGhEevc+wkIkUg2828XyZQQVar2No
	 grC4IvtEl3HOBpyews8ezymGQM3PaB/0yaPOiMViV1c1eaMw/+YMD355WFHyLEWCngB9W
	 xe+biybbBR8V7qN/vWrAEOYUEiuBDtDFeQntfqfjNRXV036nSVq7ahF/se3kzD/d++g==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; 
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=Vhq21IL6;
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 7CB2B60A261E;
	Mon, 19 Oct 2026 17:08:54 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=Vhq21IL6
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792429733;
	b=tMIFQFLPgwiVHJDSdSu3jQpEdalnHYif42y1Ix47O572oblAmRPbZtIXqIa8zOGQ8K40
	 NGIMIoA13KskxNiExjrtzu1vmjpfggv1iCi4ML+WLAnTQBNWkWVGkAW6IfnVDnYHxehxL
	 WT8Kj4WEtvNuGbI5epasFLVggEawNSqVHt1mliHXcRyoLuJ+9BbB6wpzXq37Kc5/l/HZE
	 bCR14NSTGzECjpLDYD18Kia9o+Qf36sLEY9TzAFs6QbuRa9ZlVZJznm7n2muSP9k5vHOK
	 2HcziJAOqE+Aw+L2K2CF6AbaRoim3z8xtw6JCcR5BZ+WzZvoFWnK8s/LMWlfEThYY5g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792429733;
	h=DMARC-Filter:Received:DKIM-Signature:Received:Received:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=b/+LLk0g1xg6DsRCjMqT66ZWRjvKGdYEhmiOljPoLAI=;
	b=2X9OmK7eGNhPEHBMfbR9i/DKuY9rXYLh+lA67i/mOXHVsFX1NDiSAT8o6m9akr/yLsWs
	 /gw5Hjdn38uuVWXtRNQRj1pyc7mds2bx8qnJH+iMfs1qsx3YTfmqpe3Rwp8Adhq2gPkiF
	 M+dbvUrrfjOWYK+MxNP3KXaWO+SyAW24HAQVze8Ol15QviiY7LVrMVVENON+GhBQKykiN
	 DfPMXfDHTHrjd4wQrHzw2cNzOLTEWB+xSBv6pxX3oAbMW+zScFk9De3I/2mEGeLKZurD7
	 hEQ2Tz+9PTOAGVKGqRgfRPjlMFB+R86qL+8gJuepuTwPicTDPLMzarmHeuPq3zAfCUg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=Vhq21IL6;
	arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BC20606A338;
	Mon, 19 Oct 2026 17:08:52 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6BC20606A338
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de
X-Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2])
	(Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de)
	by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 27677202856;
	Mon, 19 Oct 2026 19:08:37 +0200 (CEST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1792429717;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=b/+LLk0g1xg6DsRCjMqT66ZWRjvKGdYEhmiOljPoLAI=;
	b=Vhq21IL6K5V921IovVeQU8d5ff4gdyTjPlNjdywMx33d0N6POvw2oc4phRjGgdG/3NtuyT
	3Y25aYel4RZhKdvHpUGoBDKW2lfpsqAjGa4cRCjhulCr+iGuV78uVukxSdvhzK5E6bQ+kp
	+UmHKfF6tCBDmZ2NCdlNm7zHEdbdeW5jrY3ueTFF+po8Qfug+4en4CM5TLHhVHFSylSBmJ
	MHUTxf1auW/txB8H6rpFq2UwIt1t9/oOKCC0YdUTGbMzvD1N8ncm4N3putP+rQpnn5lU38
	AgHLQ+Z3w67qXWgXB0uxUz2uWsK00vtbh3seB5cpAtoQMtCmEp1WX4VVZpfv4w==
X-ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1792429717; a=rsa-sha256;
	cv=none;
	b=SjxEjTbY+80/FoLttNCyZrMxtqB7YKhb3RBSLADFCbiCQ0vDMIx5rJYD5G6wo+uF1nDGmh
	/ABh0E5ncPezd4Y+vY6CnpLlcjP+sFObE/fUGiF7rar1Ms1JQk9DJ6D81xl8ffiXK2J32+
	LzHmQZmTcAfbDrW3YHIMuxF02uNeMByuI0g9yOj0K2xcglBThxhxKiyitUWDuqWHpqEwhA
	b/R6fcMApYBxRFoqkh3cokXKGhtf52PsiCLiyGiNVmf47WHZSmqsg2Ob0MC36zT5TYchvO
	MwWc6BWwlK3IwWIyyzUUFnvQen0g7wqoyM2trldiX/dbqKoW98vg4mxd6vD3BQ==
X-ARC-Authentication-Results: i=1;
	ORIGINATING;
	auth=pass smtp.auth=relay-from-drehscheibe.grey.stw.pengutronix.de smtp.mailfrom=mkl@pengutronix.de
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1792429717;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=b/+LLk0g1xg6DsRCjMqT66ZWRjvKGdYEhmiOljPoLAI=;
	b=A0hxEsI8BYUqhflZRZtOzj9egPto6LQo6H/7lOw/2KGk1uTI+tnH2oxGN96xyH8ToXXnLo
	jmV/u5NALPKQbweG78fjFA5NnC9DfJD0ZyaRu+hw0BSaJtBIMqx/IhhqfmZ/KWUVC3dXYF
	LPDMTzcrLYw3HAdXYc0l8S+wtX0FwlfKJmwLUcpz2RrNdCzNRfaiad7PQa20n2SdcZBl5P
	1MKGamLJyuhlvL90brKGec4kPjBurfG+sQeywxHOu4Iig2Lkb7agy49+E0y8wwgz67xWOY
	Tl4Iis0TWNIlNPB84IU1/XHqJDby2iQgbnIqleFRawFkxPUVzCReFkcHGZaB8Q==
X-Received: from moin.white.stw.pengutronix.de ([2a0a:edc0:0:b01:1d::7b] helo=bjornoya.blackshift.org)
	by drehscheibe.grey.stw.pengutronix.de with esmtps  (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
	(Exim 4.96)
	(envelope-from <mkl@pengutronix.de>)
	id 1xIqqi-000FgM-3D;
	Mon, 19 Oct 2026 19:08:37 +0200
X-Received: from blackshift.org (p4ffb23c7.dip0.t-ipconnect.de [79.251.35.199])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(Client did not present a certificate)
	(Authenticated sender: mkl-all@blackshift.org)
	by smtp.blackshift.org (Postfix) with ESMTPSA id BBF225C9491;
	Mon, 19 Oct 2026 17:08:36 +0000 (UTC)
X-Cc: davem@davemloft.net,
	kuba@kernel.org,
	linux-can@vger.kernel.org,
	kernel@pengutronix.de,
	Runyu Xiao <runyu.xiao@seu.edu.cn>,
	stable@vger.kernel.org,
	Oliver Hartkopp <socketcan@hartkopp.net>,
	Marc Kleine-Budde <mkl@pengutronix.de>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <20261019170834.1727150-1-mkl@pengutronix.de>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list