[PATCH] compiler.h: Use compound statement for WRITE_ONCE

From: Lucas De Marchi
Date: Fri Jun 17 2022 - 05:05:19 EST


Instead of a do { } while (0), replace the outer wrapper with compound
statement. It allows to use WRITE_ONCE() inside a _Generic() without
exploding the build with the following error:

CC [M] drivers/gpu/drm/i915/gt/uc/intel_guc_ads.o
In file included from ./arch/x86/include/generated/asm/rwonce.h:1,
from ./include/linux/compiler.h:248,
from ./include/linux/build_bug.h:5,
from ./include/linux/bitfield.h:10,
from ./drivers/gpu/drm/i915/i915_reg_defs.h:9,
from ./drivers/gpu/drm/i915/gt/intel_engine_regs.h:9,
from drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c:8:
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c: In function ‘guc_policies_init’:
./include/asm-generic/rwonce.h:59:1: error: expected expression before ‘do’
59 | do { \
| ^~
./include/linux/iosys-map.h:367:13: note: in expansion of macro ‘WRITE_ONCE’
367 | u8: WRITE_ONCE(*((u8 *)vaddr__), val__), \
| ^~~~~~~~~~
./include/linux/iosys-map.h:412:17: note: in expansion of macro ‘__iosys_map_wr_sys’
412 | __iosys_map_wr_sys(val, (map__)->vaddr + (offset__), type__); \
| ^~~~~~~~~~~~~~~~~~
./include/linux/iosys-map.h:500:9: note: in expansion of macro ‘iosys_map_wr’
500 | iosys_map_wr(map__, struct_offset__ + offsetof(struct_type__, field__), \
| ^~~~~~~~~~~~
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c:68:9: note: in expansion of macro ‘iosys_map_wr_field’
68 | iosys_map_wr_field(&(guc_)->ads_map, 0, struct __guc_ads_blob, \
| ^~~~~~~~~~~~~~~~~~
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c:145:9: note: in expansion of macro ‘ads_blob_write’
145 | ads_blob_write(guc, policies.dpc_promote_time,
| ^~~~~~~~~~~~~~

One alternative would be to wrap the WRITE_ONCE in a compound statement
inside the _Generic(), but there is no downside on simply replacing the
do { } while (0).

Signed-off-by: Lucas De Marchi <lucas.demarchi@xxxxxxxxx>
---

I hit the above error when adding some additional changes in
include/linux/iosys-map.h. After changing WRITE_ONCE() and checking its
implementation I realized it would be simpler not using a _Generic() for
the WRITE_ONCE, like in
https://lore.kernel.org/dri-devel/20220617085204.1678035-2-lucas.demarchi@xxxxxxxxx/T/#u

So this patch is not strictly needed, but may be desirable for future
users.

include/asm-generic/rwonce.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h
index 8d0a6280e982..6a94c1f21648 100644
--- a/include/asm-generic/rwonce.h
+++ b/include/asm-generic/rwonce.h
@@ -56,10 +56,10 @@ do { \
} while (0)

#define WRITE_ONCE(x, val) \
-do { \
+({ \
compiletime_assert_rwonce_type(x); \
__WRITE_ONCE(x, val); \
-} while (0)
+})

static __no_sanitize_or_inline
unsigned long __read_once_word_nocheck(const void *addr)
--
2.36.1