Skip to contentSkip to conversation

compiler.h: simplify data_race() macro

PATCH Superseded Alexey Dobriyan 3 participants 9 messages Jun 24, 2024 – Jun 28, 2024 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compiler.h: simplify data_race() macro

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

---

 include/linux/compiler.h |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/compiler.h +1 -4
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)							\
 ({									\
-	__unqual_scalar_typeof(({ expr; })) __v = ({			\
-		__kcsan_disable_current();				\
-		expr;							\
-	});								\
+	__auto_type __v = (__kcsan_disable_current(), expr);		\
 	__kcsan_enable_current();					\
 	__v;								\
 })
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Alexey Dobriyan

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

Thanks for cleaning up. That code certainly predates the availability of __auto_type. Although if I recall correctly, __unqual_scalar_typeof became the first user of _Generic (the first C11 keyword we used in the kernel?), but we used some ifdef to still support ancient compilers initially (that definitely also didn't have __auto_type).

Which tree is this for?

Quoted 19 lines

---

 include/linux/compiler.h |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/compiler.h +1 -4
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __auto_type __v = (__kcsan_disable_current(), expr);            \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })
#2 lore raw
Quoted 19 lines · Marco Elver
Quoted 11 lines · Alexey Dobriyan

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

Thanks for cleaning up. That code certainly predates the availability of __auto_type. Although if I recall correctly, __unqual_scalar_typeof became the first user of _Generic (the first C11 keyword we used in the kernel?), but we used some ifdef to still support ancient compilers initially (that definitely also didn't have __auto_type).

Quoted 3 lines

Which tree is this for?

This is against mainline. Should not really matter, -next has the same code.

Quoted 14 lines
Quoted 14 lines
include/linux/compiler.h +1 -4
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __auto_type __v = (__kcsan_disable_current(), expr);            \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })
#3 lore raw
Quoted 21 lines · Alexey Dobriyan
Quoted 19 lines · Marco Elver
Quoted 11 lines · Alexey Dobriyan

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

Thanks for cleaning up. That code certainly predates the availability of __auto_type. Although if I recall correctly, __unqual_scalar_typeof became the first user of _Generic (the first C11 keyword we used in the kernel?), but we used some ifdef to still support ancient compilers initially (that definitely also didn't have __auto_type).

Quoted 5 lines
Quoted 3 lines

Which tree is this for?

This is against mainline. Should not really matter, -next has the same code.

I meant which maintainer/tree should pick this up?

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
include/linux/compiler.h +1 -4
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __auto_type __v = (__kcsan_disable_current(), expr);            \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })
#4 lore raw
In reply to Marco Elver
Quoted 28 lines · Marco Elver
Quoted 11 lines · Alexey Dobriyan

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

Thanks for cleaning up. That code certainly predates the availability of __auto_type. Although if I recall correctly, __unqual_scalar_typeof became the first user of _Generic (the first C11 keyword we used in the kernel?), but we used some ifdef to still support ancient compilers initially (that definitely also didn't have __auto_type).

Which tree is this for?

Quoted 5 lines

---

 include/linux/compiler.h |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
Quoted 14 lines
Quoted 4 lines
include/linux/compiler.h +0 -0
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */

include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 206

Quoted 10 lines
include/linux/compiler.h +1 -4
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __auto_type __v = (__kcsan_disable_current(), expr);            \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })

I just realized, comma expression should not be necesary. -Wdeclaration-after-statement prohibited simple

	({
		__kcsan_disable_current
		auto v = (expr);
		__kcsan_enable_current
		v;
	})
#5 lore raw
Quoted 48 lines · Alexey Dobriyan
Quoted 28 lines · Marco Elver
Quoted 11 lines · Alexey Dobriyan

Use auto type deduction and comma expression to decrease macro expansion size.

__unqual_scalar_typeof() is quite wordy macro by itself.

"expr" can be arbitrary complex so not expanding it twice is good. Should be faster too because type is deduced only once from the initializer.

Thanks for cleaning up. That code certainly predates the availability of __auto_type. Although if I recall correctly, __unqual_scalar_typeof became the first user of _Generic (the first C11 keyword we used in the kernel?), but we used some ifdef to still support ancient compilers initially (that definitely also didn't have __auto_type).

Which tree is this for?

Quoted 5 lines

---

 include/linux/compiler.h |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
Quoted 14 lines
Quoted 4 lines
include/linux/compiler.h +0 -0
@@ -200,10 +200,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */

include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 206

Quoted 10 lines
include/linux/compiler.h +1 -4
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __auto_type __v = (__kcsan_disable_current(), expr);            \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })

I just realized, comma expression should not be necesary. -Wdeclaration-after-statement prohibited simple

Quoted 6 lines
        ({
                __kcsan_disable_current
                auto v = (expr);
                __kcsan_enable_current
                v;
        })

Even better.

#6 lore raw

PATCH v2 1/1 compiler.h: simplify data_race() macro

-Wdeclaration-after-statement used since forever required statement expressions to inject __kcsan_disable_current(), __kcsan_enable_current() to mark data race. Now that it is gone, make macro expansion simpler.

__unqual_scalar_typeof() is wordy macro by itself. "expr" is expanded twice.

---

 include/linux/compiler.h |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/compiler.h +2 -4
@@ -200,10 +200,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)							\
 ({									\
-	__unqual_scalar_typeof(({ expr; })) __v = ({			\
-		__kcsan_disable_current();				\
-		expr;							\
-	});								\
+	__kcsan_disable_current();					\
+	__auto_type __v = (expr);					\
 	__kcsan_enable_current();					\
 	__v;								\
 })
Reviewed-by on v2 1/1
#7 lore raw
Quoted 9 lines · Alexey Dobriyan

-Wdeclaration-after-statement used since forever required statement expressions to inject __kcsan_disable_current(), __kcsan_enable_current() to mark data race. Now that it is gone, make macro expansion simpler.

__unqual_scalar_typeof() is wordy macro by itself. "expr" is expanded twice.

Quoted 20 lines

---

 include/linux/compiler.h |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/compiler.h +2 -4
@@ -200,10 +200,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)                                                        \
 ({                                                                     \
-       __unqual_scalar_typeof(({ expr; })) __v = ({                    \
-               __kcsan_disable_current();                              \
-               expr;                                                   \
-       });                                                             \
+       __kcsan_disable_current();                                      \
+       __auto_type __v = (expr);                                       \
        __kcsan_enable_current();                                       \
        __v;                                                            \
 })
#8 lore raw
In reply to Alexey Dobriyan

From: Alexey Dobriyan

Quoted 20 lines

Sent: 24 June 2024 16:40

-Wdeclaration-after-statement used since forever required statement expressions to inject __kcsan_disable_current(), __kcsan_enable_current() to mark data race. Now that it is gone, make macro expansion simpler.

__unqual_scalar_typeof() is wordy macro by itself. "expr" is expanded twice.

---

 include/linux/compiler.h |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/compiler.h +0 -0
@@ -200,10 +200,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  */
 #define data_race(expr)							\

include/linux/compiler.h void ftrace_likely_update(struct ftrace_likely_data *f, int val, · line 207

Quoted 10 lines
include/linux/compiler.h +2 -4
 ({									\
-	__unqual_scalar_typeof(({ expr; })) __v = ({			\
-		__kcsan_disable_current();				\
-		expr;							\
-	});								\
+	__kcsan_disable_current();					\
+	__auto_type __v = (expr);					\
 	__kcsan_enable_current();					\
 	__v;								\
 })

I think you can remove a lot of the tabs...

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)