From nobody Tue Feb 10 01:17:55 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 0FC0C2253FC for ; Thu, 19 Dec 2024 13:25:55 +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=1734614757; cv=none; b=ra7AdoMWJOjdXDvdEgkpHOb8Y78k86yH8T0kg5WRvrNFTekFBuaUdPZG4sA8phQpViGBSeVhsoXAJTF0VriJbvSA5c60Fn1Tl26wJmOBYYkZllLAi2DCAKFCnjenDpPNAs/QjKmLsKeneGav6z7YDudbGi6JrxRY4s3HKPbF4pg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734614757; c=relaxed/simple; bh=njLUNVG87o9EtiZ+NhMDK8cl3t/mwBSSfynfcR8M9u8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZNyIvTh+kwQbPT9WiNnDx/S+aHy5tdH8Mc3RH1gdZ16wa4hX6XvZhC7UYKZyGp1lfS5J76XCILOEfOUSd1q9lis/Ciul5A/hvrI7GCbWv3jIiouj9ScUqM1drOFzvCN4ns8NspQwn0ylXVeq97XhvbKenT3y2Xm4L3aQ9M6sA+w= 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 1tOGX5-0001bB-Og; Thu, 19 Dec 2024 14:25:39 +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 1tOGX0-004DDG-23; Thu, 19 Dec 2024 14:25:35 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tOGX1-0032im-12; Thu, 19 Dec 2024 14:25:35 +0100 From: Oleksij Rempel To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Andrew Lunn , Heiner Kallweit , Jonathan Corbet Cc: Oleksij Rempel , Mateusz Polchlopek , 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 v2 6/8] ethtool: add helper to prevent invalid statistics exposure to userspace Date: Thu, 19 Dec 2024 14:25:32 +0100 Message-Id: <20241219132534.725051-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241219132534.725051-1-o.rempel@pengutronix.de> References: <20241219132534.725051-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 --- changes v2: - add "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 4bf70cfec826..c0d3e3f62faf 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