Re: [PATCH 1/7] debugfs: Add debugfs_create_xul() for hexadecimal unsigned long

From: Joe Perches
Date: Tue Oct 22 2019 - 05:07:41 EST


On Tue, 2019-10-22 at 10:03 +0200, Geert Uytterhoeven wrote:
> Hi Joe,

Hey again Geert.

> On Mon, Oct 21, 2019 at 5:37 PM Joe Perches <joe@xxxxxxxxxxx> wrote:
> > On Mon, 2019-10-21 at 16:37 +0200, Geert Uytterhoeven wrote:
> > > The existing debugfs_create_ulong() function supports objects of
> > > type "unsigned long", which are 32-bit or 64-bit depending on the
> > > platform, in decimal form. To format objects in hexadecimal, various
> > > debugfs_create_x*() functions exist, but all of them take fixed-size
> > > types.
> > >
> > > Add a debugfs helper for "unsigned long" objects in hexadecimal format.
> > > This avoids the need for users to open-code the same, or introduce
> > > bugs when casting the value pointer to "u32 *" or "u64 *" to call
> > > debugfs_create_x{32,64}().
> > []
> > > diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h
> > []
> > > @@ -356,4 +356,14 @@ static inline ssize_t debugfs_write_file_bool(struct file *file,
> > >
> > > #endif
> > >
> > > +static inline void debugfs_create_xul(const char *name, umode_t mode,
> > > + struct dentry *parent,
> > > + unsigned long *value)
> > > +{
> > > + if (sizeof(*value) == sizeof(u32))
> > > + debugfs_create_x32(name, mode, parent, (u32 *)value);
> > > + else
> > > + debugfs_create_x64(name, mode, parent, (u64 *)value);
> >
> > trivia: the casts are unnecessary.
>
> They are necessary, in both calls (so using #ifdef as suggested below
> won't help):

Silly thinko, (I somehow thought the compiler would
eliminate the code after the branch not taken, but
of course it has to compile it first... oops)
though the #ifdef should work.

> > This might be more sensible using #ifdef
> >
> > static inline void debugfs_create_xul(const char *name, umode_t mode,
> > struct dentry *parent,
> > unsigned long *value)
> > {
> > #if BITS_PER_LONG == 64
> > debugfs_create_x64(name, mode, parent, value);
> > #else
> > debugfs_create_x32(name, mode, parent, value);
> > #endif
> > }
>
> ... at the expense of the compiler checking only one branch.
>
> Just like "if (IS_ENABLED(CONFIG_<foo>)" (when possible) is preferred
> over "#ifdef CONFIG_<foo>" because of compile-coverage, I think using
> "if" here is better than using "#if".

True if all compilers will always eliminate the unused branch.