Skip to contentSkip to conversation

tools: restore READ_ONCE() C++ compatibility

PATCH In review Mark Rutland 4 participants 9 messages Apr 4, 2018 – Apr 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: restore READ_ONCE() C++ compatibility

Our userspace <linux/compiler.h> defines READ_ONCE() in a way that clang doesn't like, as we have an anonymous union in which neither field is initialized.

WRITE_ONCE() is fine since it initializes the __val field. For READ_ONCE() we can keep clang and GCC happy with a dummy initialization of the __c field, so let's do that.

At the same time, let's split READ_ONCE() and WRITE_ONCE() over several lines for legibility, as we do in the in-kernel <linux/compiler.h>.

Fixes 6aa7de059173a986 ("locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()")
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/include/linux/compiler.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

Hi,

This is fallout from my automated ACCESS_ONCE() removal, and I'm not that familiar with using clang for perf.

In local testing, this fixes READ_ONCE() when compiling with clang, but I subsequently hit some other issues which I believe are down to LLVM API changes.

Zhijian, Sandipan, does this patch work for you?

Thanks,
Mark.

tools/include/linux/compiler.h +15 -5
index 04e32f965ad7..1827c2f973f9 100644
@@ -151,11 +151,21 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * required ordering.
  */
 
-#define READ_ONCE(x) \
-	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
-
-#define WRITE_ONCE(x, val) \
-	({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
+#define READ_ONCE(x)					\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__c = { 0 } };			\
+	__read_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+#define WRITE_ONCE(x, val)				\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__val = (val) }; 			\
+	__write_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
 
 
 #ifndef __fallthrough
Signature · 1 line
2.11.0
#1 lore raw

Hi Mark,

Quoted 3 lines · Mark Rutland

Zhijian, Sandipan, does this patch work for you?

Yes it does. Thanks for the fix.

- Sandipan

#2 lore raw
Quoted 8 lines · Sandipan Das

Hi Mark,

Quoted 3 lines · Mark Rutland

Zhijian, Sandipan, does this patch work for you?

Yes it does. Thanks for the fix.

Great! Can I take that as a Tested-by?

Thanks,
Mark.

#3 lore raw
Quoted 12 lines · Mark Rutland
Quoted 8 lines · Sandipan Das

Hi Mark,

Quoted 3 lines · Mark Rutland

Zhijian, Sandipan, does this patch work for you?

Yes it does. Thanks for the fix.

Great! Can I take that as a Tested-by?

Sure.

- Sandipan

#4 lore raw
In reply to Mark Rutland

Hi Arnaldo,

As Sandipan gave a Tested-by for this, are you happy to pick it up as a fix for v4.17?

Or would you prefer that I resend this?

Thanks,
Mark.

Quoted 68 lines · Mark Rutland

Our userspace <linux/compiler.h> defines READ_ONCE() in a way that clang doesn't like, as we have an anonymous union in which neither field is initialized.

WRITE_ONCE() is fine since it initializes the __val field. For READ_ONCE() we can keep clang and GCC happy with a dummy initialization of the __c field, so let's do that.

At the same time, let's split READ_ONCE() and WRITE_ONCE() over several lines for legibility, as we do in the in-kernel <linux/compiler.h>.

Fixes 6aa7de059173a986 ("locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()")
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/include/linux/compiler.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

Hi,

This is fallout from my automated ACCESS_ONCE() removal, and I'm not that familiar with using clang for perf.

In local testing, this fixes READ_ONCE() when compiling with clang, but I subsequently hit some other issues which I believe are down to LLVM API changes.

Zhijian, Sandipan, does this patch work for you?

Thanks,
Mark.

tools/include/linux/compiler.h +15 -5
index 04e32f965ad7..1827c2f973f9 100644
@@ -151,11 +151,21 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * required ordering.
  */
 
-#define READ_ONCE(x) \
-	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
-
-#define WRITE_ONCE(x, val) \
-	({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
+#define READ_ONCE(x)					\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__c = { 0 } };			\
+	__read_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+#define WRITE_ONCE(x, val)				\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__val = (val) }; 			\
+	__write_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
 
 
 #ifndef __fallthrough
Signature · 1 line
2.11.0
#5 lore raw

Em Mon, Apr 09, 2018 at 06:10:41PM +0100, Mark Rutland escreveu:

Quoted 6 lines

Hi Arnaldo,

As Sandipan gave a Tested-by for this, are you happy to pick it up as a fix for v4.17?

Or would you prefer that I resend this?

I forgot about this fix, but was exposed to it while processing Sandipan's patches for fixing up builtin clang support, so I ended up adding the following patch:

https://git.kernel.org/acme/c/ad0902e0c400

This sidesteps this issue by removing the sequence of includes that ends up including the compiler.h from a C++ file.

Now 'make LIBCLANGLLVM=1 -C tools/perf' works, but I'll look at the patch below, probably it will save some time in the future if we get to include compiler.h from C++ code again...

Take a look at my perf/urgent branch, that I just asked Ingo to pull.

- Arnaldo

Quoted 72 lines

Thanks,
Mark.

Quoted 68 lines · Mark Rutland

Our userspace <linux/compiler.h> defines READ_ONCE() in a way that clang doesn't like, as we have an anonymous union in which neither field is initialized.

WRITE_ONCE() is fine since it initializes the __val field. For READ_ONCE() we can keep clang and GCC happy with a dummy initialization of the __c field, so let's do that.

At the same time, let's split READ_ONCE() and WRITE_ONCE() over several lines for legibility, as we do in the in-kernel <linux/compiler.h>.

Fixes 6aa7de059173a986 ("locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()")
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/include/linux/compiler.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

Hi,

This is fallout from my automated ACCESS_ONCE() removal, and I'm not that familiar with using clang for perf.

In local testing, this fixes READ_ONCE() when compiling with clang, but I subsequently hit some other issues which I believe are down to LLVM API changes.

Zhijian, Sandipan, does this patch work for you?

Thanks,
Mark.

tools/include/linux/compiler.h +15 -5
index 04e32f965ad7..1827c2f973f9 100644
@@ -151,11 +151,21 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * required ordering.
  */
 
-#define READ_ONCE(x) \
-	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
-
-#define WRITE_ONCE(x, val) \
-	({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
+#define READ_ONCE(x)					\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__c = { 0 } };			\
+	__read_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+#define WRITE_ONCE(x, val)				\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__val = (val) }; 			\
+	__write_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
 
 
 #ifndef __fallthrough
Signature · 1 line
2.11.0
#6 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Mon, Apr 09, 2018 at 06:10:41PM +0100, Mark Rutland escreveu:

Quoted 6 lines

Hi Arnaldo,

As Sandipan gave a Tested-by for this, are you happy to pick it up as a fix for v4.17?

Or would you prefer that I resend this?

I forgot about this fix, but was exposed to it while processing Sandipan's patches for fixing up builtin clang support, so I ended up adding the following patch:

https://git.kernel.org/acme/c/ad0902e0c400

Quoted 8 lines

This sidesteps this issue by removing the sequence of includes that ends up including the compiler.h from a C++ file.

Now 'make LIBCLANGLLVM=1 -C tools/perf' works, but I'll look at the patch below, probably it will save some time in the future if we get to include compiler.h from C++ code again...

Take a look at my perf/urgent branch, that I just asked Ingo to pull.

Ah, that's great.

Sorry for the noise!

Mark.

#7 lore raw

Em Tue, Apr 10, 2018 at 11:55:15AM +0100, Mark Rutland escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 5 lines

Now 'make LIBCLANGLLVM=1 -C tools/perf' works, but I'll look at the patch below, probably it will save some time in the future if we get to include compiler.h from C++ code again...

Take a look at my perf/urgent branch, that I just asked Ingo to pull.

Ah, that's great.

Sorry for the noise!

No noise at all, your patch is valid and I'm applying it. At some point some code will include compiler.h at some include chain from a C++ file and clang will explode once more, better fix it now :-)

- Arnaldo

#8 lore raw

[tip:perf/urgent] tools headers: Restore READ_ONCE() C++ compatibility

In reply to Mark Rutland
tools headers: Restore READ_ONCE() C++ compatibility
Commit-ID:  4d3b57da1593c66835d8e3a757e4751b35493fb8
Gitweb:     https://git.kernel.org/tip/4d3b57da1593c66835d8e3a757e4751b35493fb8
Author:     Mark Rutland <[email protected]>
AuthorDate: Wed, 4 Apr 2018 17:34:45 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 12 Apr 2018 09:30:09 -0300

tools headers: Restore READ_ONCE() C++ compatibility

Our userspace <linux/compiler.h> defines READ_ONCE() in a way that clang doesn't like, as we have an anonymous union in which neither field is initialized.

WRITE_ONCE() is fine since it initializes the __val field. For READ_ONCE() we can keep clang and GCC happy with a dummy initialization of the __c field, so let's do that.

At the same time, let's split READ_ONCE() and WRITE_ONCE() over several lines for legibility, as we do in the in-kernel <linux/compiler.h>.

Fixes 6aa7de059173a986 ("locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()")
---
 tools/include/linux/compiler.h | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
tools/include/linux/compiler.h +15 -5
index 04e32f965ad7..1827c2f973f9 100644
@@ -151,11 +151,21 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  * required ordering.
  */
 
-#define READ_ONCE(x) \
-	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
-
-#define WRITE_ONCE(x, val) \
-	({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
+#define READ_ONCE(x)					\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__c = { 0 } };			\
+	__read_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+#define WRITE_ONCE(x, val)				\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__val = (val) }; 			\
+	__write_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
 
 
 #ifndef __fallthrough