Re: [PATCH] asm-generic/ioctl.h: use BUILD_BUG_ON_ZERO() for type check

From: Masahiro Yamada
Date: Tue Feb 23 2021 - 20:59:13 EST


On Wed, Feb 24, 2021 at 5:04 AM Arnd Bergmann <arnd@xxxxxxxxxx> wrote:
>
> On Tue, Feb 23, 2021 at 11:06 AM Masahiro Yamada <masahiroy@xxxxxxxxxx> wrote:
>
> >
> > -#ifdef __CHECKER__
> > -#define _IOC_TYPECHECK(t) (sizeof(t))
> > -#else
> > /* provoke compile error for invalid uses of size argument */
> > -extern unsigned int __invalid_size_argument_for_IOC;
> > +#undef _IOC_TYPECHECK
> > #define _IOC_TYPECHECK(t) \
> > - ((sizeof(t) == sizeof(t[1]) && \
> > - sizeof(t) < (1 << _IOC_SIZEBITS)) ? \
> > - sizeof(t) : __invalid_size_argument_for_IOC)
> > -#endif
> > + BUILD_BUG_ON_ZERO(sizeof(t) != sizeof(t[1]) || \
> > + sizeof(t) >= (1 << _IOC_SIZEBITS))
>
> Using BUILD_BUG_ON_ZERO sounds like a good idea
>
> > #endif /* _ASM_GENERIC_IOCTL_H */
> > diff --git a/include/uapi/asm-generic/ioctl.h b/include/uapi/asm-generic/ioctl.h
> > index a84f4db8a250..d50bd39ec3e3 100644
> > --- a/include/uapi/asm-generic/ioctl.h
> > +++ b/include/uapi/asm-generic/ioctl.h
> > @@ -72,9 +72,8 @@
> > ((nr) << _IOC_NRSHIFT) | \
> > ((size) << _IOC_SIZESHIFT))
> >
> > -#ifndef __KERNEL__
> > -#define _IOC_TYPECHECK(t) (sizeof(t))
> > -#endif
> > +#define _IOC_TYPECHECK(t) 0
> > +#define _IOC_SIZE_WITH_TYPECHECK(t) (sizeof(t) + _IOC_TYPECHECK(t))
>
> But I think replacing the #ifndef with an #undef in the other file makes it
> harder to understand when reading through it and trying to understand
> what it would do when this gets included from kernel and user space.
>
> Arnd


My intention is to improve the UAPI/KAPI decoupling
to decrease the task of scripts/headers_install.sh

Ideally, we could export UAPI headers with
almost no modification.

It is true that scripts/unifdef can remove #ifndef __KERNEL__
blocks, but having the kernel-space code in UAPI headers
does not make sense. Otherwise, our initial motivation
"separate them by directory structure" would be lost.

So, I believe redefining _IOC_TYPECHECK is the right direction.
I can add comments if this is not clear.







--
Best Regards
Masahiro Yamada