[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAKwvOdm6LUFBC=as-9fTxYM=H2U=JuzquanFYwasn2Vu3TgsxA@mail.gmail.com>
Date: Fri, 15 Oct 2021 10:33:50 -0700
From: Nick Desaulniers <ndesaulniers@...gle.com>
To: Rasmus Villemoes <linux@...musvillemoes.dk>
Cc: akpm@...ux-foundation.org,
Miguel Ojeda <miguel.ojeda.sandonis@...il.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] linux/container_of.h: switch to static_assert
On Fri, Oct 15, 2021 at 2:05 AM Rasmus Villemoes
<linux@...musvillemoes.dk> wrote:
>
> _Static_assert() is evaluated already in the compiler's frontend, and
> gives a somehat more to-the-point error, compared to the BUILD_BUG_ON
> macro, which only fires after the optimizer has had a chance to
> eliminate calls to functions marked with
> __attribute__((error)). In theory, this might make builds a tiny bit
> faster.
>
> There's also a little less gunk in the error message emitted:
>
> lib/sort.c: In function ‘foo’:
> ./include/linux/build_bug.h:78:41: error: static assertion failed: "pointer type mismatch in container_of()"
> 78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
>
> compared to
>
> lib/sort.c: In function ‘foo’:
> ././include/linux/compiler_types.h:322:38: error: call to ‘__compiletime_assert_2’ declared with attribute error: pointer type mismatch in container_of()
> 322 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
>
> While at it, fix the copy-pasto in container_of_safe().
>
> Signed-off-by: Rasmus Villemoes <linux@...musvillemoes.dk>
Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@...gle.com>
> ---
> akpm: This is obviously on top of Andy's kernel.h splitup series, so
> should go along with those if acked.
>
> include/linux/container_of.h | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/container_of.h b/include/linux/container_of.h
> index dd56019838c6..2f4944b791b8 100644
> --- a/include/linux/container_of.h
> +++ b/include/linux/container_of.h
> @@ -16,9 +16,9 @@
> */
> #define container_of(ptr, type, member) ({ \
> void *__mptr = (void *)(ptr); \
> - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
> - !__same_type(*(ptr), void), \
> - "pointer type mismatch in container_of()"); \
> + static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> + __same_type(*(ptr), void), \
> + "pointer type mismatch in container_of()"); \
> ((type *)(__mptr - offsetof(type, member))); })
>
> /**
> @@ -31,9 +31,9 @@
> */
> #define container_of_safe(ptr, type, member) ({ \
> void *__mptr = (void *)(ptr); \
> - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
> - !__same_type(*(ptr), void), \
> - "pointer type mismatch in container_of()"); \
> + static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> + __same_type(*(ptr), void), \
> + "pointer type mismatch in container_of_safe()"); \
> IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
> ((type *)(__mptr - offsetof(type, member))); })
>
> --
> 2.31.1
>
--
Thanks,
~Nick Desaulniers
Powered by blists - more mailing lists