Re: [PATCH 1/2] debugfs: improve DEFINE_DEBUGFS_ATTRIBUTE for !CONFIG_DEBUGFS_FS

From: Arnd Bergmann
Date: Thu Oct 13 2016 - 06:36:49 EST


On Thursday, October 13, 2016 11:59:54 AM CEST Nicolai Stange wrote:
> >
> > +ssize_t debugfs_attr_read(struct file *file, char __user *buf,
> > + size_t len, loff_t *ppos);
> > +ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
> > + size_t len, loff_t *ppos);
> > +
> > +#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
> > +static int __fops ## _open(struct inode *inode, struct file *file) \
> > +{ \
> > + __simple_attr_check_format(__fmt, 0ull); \
> > + return simple_attr_open(inode, file, __get, __set, __fmt); \
> > +} \
> > +static const struct file_operations __fops = { \
> > + .owner = THIS_MODULE, \
> > + .open = __fops ## _open, \
> > + .release = simple_attr_release, \
> > + .read = debugfs_attr_read, \
> > + .write = debugfs_attr_write, \
>
> This depends on GCC dead code elimination to always work for this
> situation, otherwise we'd get undefined references to
> debugfs_attr_read/write(), right?

Correct.

> In order to avoid having to test your patch against all those older
> versions of GCC, can we have a safety net here and define some dummy
> debugfs_attr_read/write() for the !CONFIG_DEBUGFS case?

The question of dead-code elimination in older gcc versions comes up
occasionally, and I think all versions that are able to build the
kernel these days get this right all the time, otherwise any code
using IS_ENABLED() helpers to control the calling of external interfaces
would be broken.

We could probably use that macro here if you think that's better
and do:

static const struct file_operations __fops = {
.owner = THIS_MODULE,
.open = IS_ENABLED(CONFIG_DEBUGFS_FS) ? __fops ## _open : NULL,
...

> If nothing else, it would IMHO make the !CONFIG_DEBUGFS case more
> understandable because one had not to figure out that this actually
> relies on dead code elimination to work.

Sure, that's fine. Can you do the new version of that patch with
the change then?

Arnd