Skip to content

Commit dee081b

Browse files
committed
READ_ONCE: Drop pointer qualifiers when reading from scalar types
Passing a volatile-qualified pointer to READ_ONCE() is an absolute trainwreck for code generation: the use of 'typeof()' to define a temporary variable inside the macro means that the final evaluation in macro scope ends up forcing a read back from the stack. When stack protector is enabled (the default for arm64, at least), this causes the compiler to vomit up all sorts of junk. Unfortunately, dropping pointer qualifiers inside the macro poses quite a challenge, especially since the pointed-to type is permitted to be an aggregate, and this is relied upon by mm/ code accessing things like 'pmd_t'. Based on numerous hacks and discussions on the mailing list, this is the best I've managed to come up with. Introduce '__unqual_scalar_typeof()' which takes an expression and, if the expression is an optionally qualified 8, 16, 32 or 64-bit scalar type, evaluates to the unqualified type. Other input types, including aggregates, remain unchanged. Hopefully READ_ONCE() on volatile aggregate pointers isn't something we do on a fast-path. Cc: Peter Zijlstra <[email protected]> Cc: Arnd Bergmann <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Reported-by: Michael Ellerman <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 9e343b4 commit dee081b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

include/linux/compiler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
203203
* atomicity or dependency ordering guarantees. Note that this may result
204204
* in tears!
205205
*/
206-
#define __READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
206+
#define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
207207

208208
#define __READ_ONCE_SCALAR(x) \
209209
({ \
210-
typeof(x) __x = __READ_ONCE(x); \
210+
__unqual_scalar_typeof(x) __x = __READ_ONCE(x); \
211211
smp_read_barrier_depends(); \
212-
__x; \
212+
(typeof(x))__x; \
213213
})
214214

215215
#define READ_ONCE(x) \

include/linux/compiler_types.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,27 @@ struct ftrace_likely_data {
210210
/* Are two types/vars the same type (ignoring qualifiers)? */
211211
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
212212

213+
/*
214+
* __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
215+
* non-scalar types unchanged.
216+
*
217+
* We build this out of a couple of helper macros in a vain attempt to
218+
* help you keep your lunch down while reading it.
219+
*/
220+
#define __pick_scalar_type(x, type, otherwise) \
221+
__builtin_choose_expr(__same_type(x, type), (type)0, otherwise)
222+
223+
#define __pick_integer_type(x, type, otherwise) \
224+
__pick_scalar_type(x, unsigned type, \
225+
__pick_scalar_type(x, signed type, otherwise))
226+
227+
#define __unqual_scalar_typeof(x) typeof( \
228+
__pick_integer_type(x, char, \
229+
__pick_integer_type(x, short, \
230+
__pick_integer_type(x, int, \
231+
__pick_integer_type(x, long, \
232+
__pick_integer_type(x, long long, x))))))
233+
213234
/* Is this type a native word size -- useful for atomic operations */
214235
#define __native_word(t) \
215236
(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \

0 commit comments

Comments
 (0)