From patchwork Sat Dec 21 08:15:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13917710 X-Patchwork-Delegate: kuba@kernel.org 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 E8CC41F9ED8 for ; Sat, 21 Dec 2024 08:15:56 +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=1734768958; cv=none; b=pcGcNHwaTGtijo/r4w4JIIoDDuSH5BtktKoiLXbm7GCk/9Jb0vjOEavG3XUn/vlsijLOepvVMQJXueHWjsrGQpbgjzPXMcG5+diDwEO8DR7sAfXFuSZtrSIgl4nD1kshCABqtyn94+39D9mqohzPiquhBc7zlal7wRXu7gJpl1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734768958; c=relaxed/simple; bh=njLUNVG87o9EtiZ+NhMDK8cl3t/mwBSSfynfcR8M9u8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rUycv0sduTmz8m6o8C8/nbixnzMwLr8IALOzZxXepho3ouRH7vvckJkF5rOlvp/mRkzM7MgzCzEkC8G4wTanwG9QDBEmX6WcTTAJo5QTQSB+aAu/7dskgEHW520BOVZraVzbcddGzqui8+bk1Bc199Y1dsGBE79eqfEvxCy5YT4= 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 1tOue8-0000Qs-3z; Sat, 21 Dec 2024 09:15:36 +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 1tOue3-004Vdy-02; Sat, 21 Dec 2024 09:15:31 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tOue3-00CbTY-2D; Sat, 21 Dec 2024 09:15:31 +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 v4 6/8] ethtool: add helper to prevent invalid statistics exposure to userspace Date: Sat, 21 Dec 2024 09:15:28 +0100 Message-Id: <20241221081530.3003900-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241221081530.3003900-1-o.rempel@pengutronix.de> References: <20241221081530.3003900-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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, unsigned int n) stats[n] = ETHTOOL_STAT_NOT_SET; } +/** + * ethtool_stat_add - Add a value to a u64 statistic with wraparound handling + * @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 addition + * equals the reserved value (`ETHTOOL_STAT_NOT_SET`), it increments the result + * by 1 to avoid the reserved value. + */ +static inline void ethtool_stat_add(u64 *stat, u64 value) +{ + *stat += value; + if (*stat == ETHTOOL_STAT_NOT_SET) + (*stat)++; +} + /* Basic IEEE 802.3 MAC statistics (30.3.1.1.*), not otherwise exposed * via a more targeted API. */