Re: [PATCH 1/2] net: Enable 64-bit net device statistics on 32-bit architectures

Ben Hutchings
2010-06-03T19:11:42Z

On Thu, 2010-06-03 at 11:47 -0700, Stephen Hemminger wrote:
> On Thu, 03 Jun 2010 18:39:04 +0100
> Ben Hutchings <bhutchings@solarflare.com> wrote:
>
> > #if BITS_PER_LONG == 64
> > +#define NET_DEVICE_STATS_DEFINE(name) u64 name
> > +#define RTNL_LINK_STATS64_READ(stats, name) \
> > + ACCESS_ONCE((stats)->name)
> > +#define RTNL_LINK_STATS64_READ_OFFSET(stats, offset) \
> > + ACCESS_ONCE((const u64 *)((const u8 *)(stats) + (offset)))
> > +#define RTNL_LINK_STATS64_READ32(stats, name) \
> > + ((u32)ACCESS_ONCE((stats)->name))
> > +#else
> > +#if defined(__LITTLE_ENDIAN)
> > +#define NET_DEVICE_STATS_DEFINE(name) u32 name, pad_ ## name
> > +#define RTNL_LINK_STATS64_READ_OFFSET(stats, offset) \
> > + (ACCESS_ONCE(*(const u32 *)((const u8 *)(stats) + (offset))) | \
> > + (u64)(*(const u32 *)((const u8 *)(stats) + (offset) + 4)) << 32)
> > +#define RTNL_LINK_STATS64_READ32(stats, name) \
> > + (((const volatile u32 *)&(stats)->name)[0])
> > +#else
> > +#define NET_DEVICE_STATS_DEFINE(name) u32 pad_ ## name, name
> > +#define RTNL_LINK_STATS64_READ_OFFSET(stats, offset) \
> > + ((u64)(*(const u32 *)((const u8 *)(stats) + (offset))) << 32 | \
> > + ACCESS_ONCE(*(const u32 *)((const u8 *)(stats) + (offset) + 4)))
> > +#define RTNL_LINK_STATS64_READ32(stats, name) \
> > + (((const volatile u32 *)&(stats)->name)[1])
> > +#endif
> > +#define RTNL_LINK_STATS64_READ(stats, name) \
> > + RTNL_LINK_STATS64_READ_OFFSET( \
> > + stats, offsetof(struct rtnl_link_stats64, name))
> > +#endif
>
> Macros... with multiple casts. Gack

RTNL_LINK_STATS64_READ_OFFSET() is only really needed in net-sysfs.c,
and that ugliness could maybe be left there. So maybe the accessors
could be defined as something like:

#if BITS_PER_LONG == 64

static inline u64 rtnl_link_stats64_read(const u64 *field)
{
return ACCESS_ONCE(*field);
}
static inline u32 rtnl_link_stats64_read32(const u64 *field)
{
return ACCESS_ONCE(*field);
}

#else

static inline u64 rtnl_link_stats64_read(const u64 *field)
{
#if defined(__LITTLE_ENDIAN)
const u32 *low = (const u32 *)field, *high = low + 1;
#else
const u32 *high = (const u32 *)field, *low = high + 1;
#endif
return ACCESS_ONCE(*low) | (u64)*high << 32;
}
static inline u32 rtnl_link_stats64_read32(const u64 *field)
{
#if defined(__LITTLE_ENDIAN)
const u32 *low = (const u32 *)field;
#else
const u32 *low = (const u32 *)field + 1;
#endif
return ACCESS_ONCE(*low);
}

#endif

Ben.

--
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.