Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp60544lfe; Fri, 15 Apr 2022 19:30:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcOmjEbaZo4FmjTApNEyNyHwxKwK0ycPQWVLo7ksJtic6BO9VlhUwXgqLFTsMPwlXUM0SX X-Received: by 2002:a17:90a:f3d6:b0:1cb:a0aa:5e60 with SMTP id ha22-20020a17090af3d600b001cba0aa5e60mr1825901pjb.161.1650076246304; Fri, 15 Apr 2022 19:30:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650076246; cv=none; d=google.com; s=arc-20160816; b=QJlPL9FLdb4zG6q5Afsl2u6KfwBl2r6JvpnRf46kbaU+6OExysgDNnPyqS1k+4Wun/ GwvtY82shqp4T7WRONG8TlkiAH/NGnhjsi8DRWnN+CEOROzJQO1zkFPd/OSTRAfBmuqs cm/avlweGHl6NbE2mXYYsuNbaEt/4UeKnvxvsSV2iimhwZG7cgp4mKlA5U1iH9nZ2NQp VKn6nMDyJ0fu83Zx4VNAgrwIRQ7WidRZ9qoPnuo5wvv2wZk5dfgWhNHxDQJlA2nb3VZK 6JXkNF4TvFP31s8qFMgm68jN0SFLAPdbL9VaJIw6cmNxe36eOzVYgFr7yR5sF1sGoGYI VofQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=CFuW2Yfm9w5+kjCNJGdTozdGvIwhLZUxIXX3CCrU31k=; b=1BIOyHeiV+RzMLUFrHYmRcHC+P5fJiUVyucCvNwbfFy9FrcbRucWLjS/Rq2bx1IawM DDvd0iVweSb677wzgP5A1KvwnNPKOGvVElm+RgEVEEMga1bSiIphAlhMMpm/odjDs0MY ms5OrofII4HKPgv7LEEWz/FkhhjG5g5xSmLKsZx0anP/Cg3CXS+q4y8QB28zQoKysmN9 RxPFgO1Hz+7Q/XkBHpk4YqOFsC6Nxr2uUC1BE0KGmMlN/APv8S9xWRr7FXrhUozPIq0F dpXYX5y9Vytuz8C3aTuDclyeefGZzrk4DHGAXZ4b+hoF1eTyUO/kEqNLAVcqrRZFgtS9 Tg4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lunn.ch header.s=20171124 header.b=fwlAINok; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id b18-20020a639312000000b0039da754b1a1si2814752pge.151.2022.04.15.19.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:30:46 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@lunn.ch header.s=20171124 header.b=fwlAINok; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B0D5F1637CD; Fri, 15 Apr 2022 18:40:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347439AbiDNXSv (ORCPT + 99 others); Thu, 14 Apr 2022 19:18:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240552AbiDNXSt (ORCPT ); Thu, 14 Apr 2022 19:18:49 -0400 Received: from vps0.lunn.ch (vps0.lunn.ch [185.16.172.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ACCDAC907; Thu, 14 Apr 2022 16:16:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lunn.ch; s=20171124; h=In-Reply-To:Content-Transfer-Encoding:Content-Disposition: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:From: Sender:Reply-To:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Content-Disposition: In-Reply-To:References; bh=CFuW2Yfm9w5+kjCNJGdTozdGvIwhLZUxIXX3CCrU31k=; b=fw lAINokVw/geP2JhxUmI9Ru88RDoIbh3GzNFWpBnjymIOnkJr6Oh+JipuG1hcV6q+Ka9vUgP2iyc59 M8CeeMzMCbY80OrIk+qSqhPmKATuCzhUMZkQRJoPV7uz1izxLmhKwWe718/5gYSqiBzSOlHRAtOyQ Oi4wLHGvsQvDaZw=; Received: from andrew by vps0.lunn.ch with local (Exim 4.94.2) (envelope-from ) id 1nf8h9-00Ft3S-Nv; Fri, 15 Apr 2022 01:16:11 +0200 Date: Fri, 15 Apr 2022 01:16:11 +0200 From: Andrew Lunn To: =?iso-8859-1?Q?Cl=E9ment_L=E9ger?= Cc: Vivien Didelot , Florian Fainelli , Vladimir Oltean , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Geert Uytterhoeven , Magnus Damm , Heiner Kallweit , Russell King , Thomas Petazzoni , Herve Codina , =?iso-8859-1?Q?Miqu=E8l?= Raynal , Milan Stevanovic , Jimmy Lalande , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-renesas-soc@vger.kernel.org, netdev@vger.kernel.org Subject: Re: [PATCH net-next 07/12] net: dsa: rzn1-a5psw: add statistics support Message-ID: References: <20220414122250.158113-1-clement.leger@bootlin.com> <20220414122250.158113-8-clement.leger@bootlin.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220414122250.158113-8-clement.leger@bootlin.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 14, 2022 at 02:22:45PM +0200, Cl?ment L?ger wrote: > Add per-port statistics. This support requries to add a stat lock since > statistics are stored in two 32 bits registers, the hi part one being > global and latched when accessing the lo part. > > Signed-off-by: Cl?ment L?ger > --- > drivers/net/dsa/rzn1_a5psw.c | 101 +++++++++++++++++++++++++++++++++++ > drivers/net/dsa/rzn1_a5psw.h | 2 + > 2 files changed, 103 insertions(+) > > diff --git a/drivers/net/dsa/rzn1_a5psw.c b/drivers/net/dsa/rzn1_a5psw.c > index 5bee999f7050..7ab7d9054427 100644 > --- a/drivers/net/dsa/rzn1_a5psw.c > +++ b/drivers/net/dsa/rzn1_a5psw.c > @@ -16,6 +16,59 @@ > > #include "rzn1_a5psw.h" > > +struct a5psw_stats { > + u16 offset; > + const char *name; > +}; > + > +#define STAT_DESC(_offset, _name) {.offset = _offset, .name = _name} > + > +static const struct a5psw_stats a5psw_stats[] = { > + STAT_DESC(0x868, "aFrameTransmitted"), > + STAT_DESC(0x86C, "aFrameReceived"), > + STAT_DESC(0x8BC, "etherStatsetherStatsOversizePktsDropEvents"), > +}; > +static void a5psw_get_strings(struct dsa_switch *ds, int port, u32 stringset, > + uint8_t *data) > +{ > + unsigned int u; > + > + if (stringset != ETH_SS_STATS) > + return; > + > + for (u = 0; u < ARRAY_SIZE(a5psw_stats); u++) { > + strncpy(data + u * ETH_GSTRING_LEN, a5psw_stats[u].name, > + ETH_GSTRING_LEN); > + } The kernel strncpy() is like the user space one. It does not add a NULL if the string is longer than ETH_GSTRING_LEN and it needs to truncate. So there is a danger here. What you find most drivers do is struct a5psw_stats { u16 offset; const char name[ETH_GSTRING_LEN]; }; You should then get a compiler warning/error if you string is ever longer than allowed. And use memcpy() rather than strcpy(), which is faster anyway. But you do use up a bit more memory. > +static void a5psw_get_ethtool_stats(struct dsa_switch *ds, int port, > + uint64_t *data) > +{ > + struct a5psw *a5psw = ds->priv; > + u32 reg_lo, reg_hi; > + unsigned int u; > + > + for (u = 0; u < ARRAY_SIZE(a5psw_stats); u++) { > + /* A5PSW_STATS_HIWORD is global and thus, access must be > + * exclusive > + */ Could you explain that a bit more. The RTNL lock will prevent two parallel calls to this function. > + spin_lock(&a5psw->stats_lock); > + reg_lo = a5psw_reg_readl(a5psw, a5psw_stats[u].offset + > + A5PSW_PORT_OFFSET(port)); > + /* A5PSW_STATS_HIWORD is latched on stat read */ > + reg_hi = a5psw_reg_readl(a5psw, A5PSW_STATS_HIWORD); > + > + data[u] = ((u64)reg_hi << 32) | reg_lo; > + spin_unlock(&a5psw->stats_lock); > + } > +} Andrew