Subject: Re: [PATCH net-next 07/12] net: dsa: rzn1-a5psw: add statistics support

From: Clément Léger <[email-masked]>
Date: Fri, 15 Apr 2022 14:04:02 +0200

Le Fri, 15 Apr 2022 01:16:11 +0200,
Andrew Lunn <[email-masked]> a écrit :

> 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 <[email-masked]>
> > ---
> >  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.

Acked.

> 
> > +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.

Ok, I wasn't sure of the locking applicable here.

-- 
Clément Léger,
Embedded Linux and Kernel engineer at Bootlin
https://bootlin.com