linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] linux/container_of.h: switch to static_assert
@ 2021-10-15  9:05 Rasmus Villemoes
  2021-10-15 13:08 ` Miguel Ojeda
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2021-10-15  9:05 UTC (permalink / raw)
  To: akpm
  Cc: Miguel Ojeda, Andy Shevchenko, Nick Desaulniers,
	Rasmus Villemoes, linux-kernel

_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@rasmusvillemoes.dk>
---
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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux/container_of.h: switch to static_assert
  2021-10-15 13:13   ` Andy Shevchenko
@ 2021-10-15 11:46     ` Rasmus Villemoes
  0 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2021-10-15 11:46 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: akpm, Miguel Ojeda, Nick Desaulniers, linux-kernel

On 15/10/2021 15.13, Andy Shevchenko wrote:

> Btw, shouldn't we also remove build_bug.h in the container_of.h with this?
> (Or if this goes first, do we still need bug.h?)
> 

We absolutely need build_bug.h, as that is where static_assert is defined.

container_of.h doesn't include bug.h AFAICT, nor should it need to.

(We should still find a quiet place for __same_type and have
container_of include that header, but that's unrelated to this patch.)

Rasmus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux/container_of.h: switch to static_assert
  2021-10-15  9:05 [PATCH] linux/container_of.h: switch to static_assert Rasmus Villemoes
@ 2021-10-15 13:08 ` Miguel Ojeda
  2021-10-15 13:11 ` Andy Shevchenko
  2021-10-15 17:33 ` Nick Desaulniers
  2 siblings, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2021-10-15 13:08 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Andrew Morton, Andy Shevchenko, Nick Desaulniers, linux-kernel

On Fri, Oct 15, 2021 at 11:05 AM Rasmus Villemoes
<linux@rasmusvillemoes.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.

Thanks for this! Very much in favor!

Reviewed-by: Miguel Ojeda <ojeda@kernel.org>

Perhaps Andrew may want to add a link to the related discussion
thread: https://lore.kernel.org/lkml/20211014132331.GA4811@kernel.org/T/

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux/container_of.h: switch to static_assert
  2021-10-15  9:05 [PATCH] linux/container_of.h: switch to static_assert Rasmus Villemoes
  2021-10-15 13:08 ` Miguel Ojeda
@ 2021-10-15 13:11 ` Andy Shevchenko
  2021-10-15 13:13   ` Andy Shevchenko
  2021-10-15 17:33 ` Nick Desaulniers
  2 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2021-10-15 13:11 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: akpm, Miguel Ojeda, Nick Desaulniers, linux-kernel

On Fri, Oct 15, 2021 at 11:05:30AM +0200, Rasmus Villemoes 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().

Thanks, Rasmus!
Make sense to me.
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
> akpm: This is obviously on top of Andy's kernel.h splitup series, so
> should go along with those if acked.

Nevertheless, kbuild bot is not happy about bottom_half.h (_RET_IP_, _THIS_IP_
definitions). Do you have any idea what to do the best? (I think those macros
deserve a separate header as well).

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux/container_of.h: switch to static_assert
  2021-10-15 13:11 ` Andy Shevchenko
@ 2021-10-15 13:13   ` Andy Shevchenko
  2021-10-15 11:46     ` Rasmus Villemoes
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2021-10-15 13:13 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: akpm, Miguel Ojeda, Nick Desaulniers, linux-kernel

On Fri, Oct 15, 2021 at 04:11:01PM +0300, Andy Shevchenko wrote:
> On Fri, Oct 15, 2021 at 11:05:30AM +0200, Rasmus Villemoes 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().
> 
> Thanks, Rasmus!
> Make sense to me.

Btw, shouldn't we also remove build_bug.h in the container_of.h with this?
(Or if this goes first, do we still need bug.h?)

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] linux/container_of.h: switch to static_assert
  2021-10-15  9:05 [PATCH] linux/container_of.h: switch to static_assert Rasmus Villemoes
  2021-10-15 13:08 ` Miguel Ojeda
  2021-10-15 13:11 ` Andy Shevchenko
@ 2021-10-15 17:33 ` Nick Desaulniers
  2 siblings, 0 replies; 6+ messages in thread
From: Nick Desaulniers @ 2021-10-15 17:33 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: akpm, Miguel Ojeda, Andy Shevchenko, linux-kernel

On Fri, Oct 15, 2021 at 2:05 AM Rasmus Villemoes
<linux@rasmusvillemoes.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@rasmusvillemoes.dk>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-10-15 17:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-15  9:05 [PATCH] linux/container_of.h: switch to static_assert Rasmus Villemoes
2021-10-15 13:08 ` Miguel Ojeda
2021-10-15 13:11 ` Andy Shevchenko
2021-10-15 13:13   ` Andy Shevchenko
2021-10-15 11:46     ` Rasmus Villemoes
2021-10-15 17:33 ` Nick Desaulniers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).