From nobody Mon Feb 9 21:38:27 2026 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF0BD185B48 for ; Tue, 3 Dec 2024 07:56:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733212601; cv=none; b=nyrz8TK0BA7PWc0Jrwf7vvRKEsLFUm4fO/TG7jsh+LSMQ75fdkTmktv12UgL+gi9YUETCgTiOfwFBUxYTfkN0JHIziDZVIaSYnOdg+ZtqRhwplM47g75NfcdWNCs37YjEO2vDA7GITiFWpTeu7RMS/YUlbLM5b8WdS6LbxnqH0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733212601; c=relaxed/simple; bh=oSPdpmoBlXHFSd4Hu5Dq7Ep5kgm70t61USTk5+qm7rs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sH57woHlBHwMeyfZ78U8gl/sqNeA9Aui17qAyqEpR3yFHD1zTS59xrVt5b0Eh9EGvBUsZnmfFcEgKoUANnnM7S8N7j9Zu+s2Tm6V8BVFnrdWcM603icocLqXfqJokoiYe7TW0l+uVL5EPRC4E6TNW7h7jBD652YQZX8UsroA2jo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tINlj-000397-Kk; Tue, 03 Dec 2024 08:56:27 +0100 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tINle-001R92-0r; Tue, 03 Dec 2024 08:56:23 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tINle-00AHwS-33; Tue, 03 Dec 2024 08:56:22 +0100 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Andrew Lunn , Heiner Kallweit , Jonathan Corbet Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Simon Horman , Russell King , Maxime Chevallier , linux-doc@vger.kernel.org Subject: [PATCH net-next v1 5/7] ethtool: add helper to prevent invalid statistics exposure to userspace Date: Tue, 3 Dec 2024 08:56:19 +0100 Message-Id: <20241203075622.2452169-6-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241203075622.2452169-1-o.rempel@pengutronix.de> References: <20241203075622.2452169-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce a new helper function, `ethtool_stat_add`, to update 64-bit statistics with proper handling of the reserved value `ETHTOOL_STAT_NOT_SET`. This ensures that statistics remain valid and are always reported to userspace, even if the driver accidentally sets `ETHTOOL_STAT_NOT_SET` during an update. Signed-off-by: Oleksij Rempel Reviewed-by: Mateusz Polchlopek --- include/linux/ethtool.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/linux/ethtool.h b/include/linux/ethtool.h index b0ed740ca749..657bd69ddaf7 100644 --- a/include/linux/ethtool.h +++ b/include/linux/ethtool.h @@ -371,6 +371,22 @@ static inline void ethtool_stats_init(u64 *stats, unsi= gned int n) stats[n] =3D ETHTOOL_STAT_NOT_SET; } =20 +/** + * ethtool_stat_add - Add a value to a u64 statistic with wraparound handl= ing + * @stat: Pointer to the statistic to update + * @value: Value to add to the statistic + * + * Adds the specified value to a u64 statistic. If the result of the addit= ion + * equals the reserved value (`ETHTOOL_STAT_NOT_SET`), it increments the r= esult + * by 1 to avoid the reserved value. + */ +static inline void ethtool_stat_add(u64 *stat, u64 value) +{ + *stat +=3D value; + if (*stat =3D=3D ETHTOOL_STAT_NOT_SET) + (*stat)++; +} + /* Basic IEEE 802.3 MAC statistics (30.3.1.1.*), not otherwise exposed * via a more targeted API. */ --=20 2.39.5