diff options
author | Will Deacon <will.deacon@arm.com> | 2018-02-05 09:16:06 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2018-02-14 19:15:51 -0500 |
commit | 8fa80c503b484ddc1abbd10c7cb2ab81f3824a50 (patch) | |
tree | b5b1d8d3cfbb754ca59916900ba1ce50d993a8a2 | |
parent | be3233fbfcb8f5acb6e3bcd0895c3ef9e100d470 (diff) |
nospec: Move array_index_nospec() parameter checking into separate macro
For architectures providing their own implementation of
array_index_mask_nospec() in asm/barrier.h, attempting to use WARN_ONCE() to
complain about out-of-range parameters using WARN_ON() results in a mess
of mutually-dependent include files.
Rather than unpick the dependencies, simply have the core code in nospec.h
perform the checking for us.
Signed-off-by: Will Deacon <will.deacon@arm.com>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1517840166-15399-1-git-send-email-will.deacon@arm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r-- | include/linux/nospec.h | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/include/linux/nospec.h b/include/linux/nospec.h index b99bced39ac2..fbc98e2c8228 100644 --- a/include/linux/nospec.h +++ b/include/linux/nospec.h | |||
@@ -20,20 +20,6 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, | |||
20 | unsigned long size) | 20 | unsigned long size) |
21 | { | 21 | { |
22 | /* | 22 | /* |
23 | * Warn developers about inappropriate array_index_nospec() usage. | ||
24 | * | ||
25 | * Even if the CPU speculates past the WARN_ONCE branch, the | ||
26 | * sign bit of @index is taken into account when generating the | ||
27 | * mask. | ||
28 | * | ||
29 | * This warning is compiled out when the compiler can infer that | ||
30 | * @index and @size are less than LONG_MAX. | ||
31 | */ | ||
32 | if (WARN_ONCE(index > LONG_MAX || size > LONG_MAX, | ||
33 | "array_index_nospec() limited to range of [0, LONG_MAX]\n")) | ||
34 | return 0; | ||
35 | |||
36 | /* | ||
37 | * Always calculate and emit the mask even if the compiler | 23 | * Always calculate and emit the mask even if the compiler |
38 | * thinks the mask is not needed. The compiler does not take | 24 | * thinks the mask is not needed. The compiler does not take |
39 | * into account the value of @index under speculation. | 25 | * into account the value of @index under speculation. |
@@ -44,6 +30,26 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, | |||
44 | #endif | 30 | #endif |
45 | 31 | ||
46 | /* | 32 | /* |
33 | * Warn developers about inappropriate array_index_nospec() usage. | ||
34 | * | ||
35 | * Even if the CPU speculates past the WARN_ONCE branch, the | ||
36 | * sign bit of @index is taken into account when generating the | ||
37 | * mask. | ||
38 | * | ||
39 | * This warning is compiled out when the compiler can infer that | ||
40 | * @index and @size are less than LONG_MAX. | ||
41 | */ | ||
42 | #define array_index_mask_nospec_check(index, size) \ | ||
43 | ({ \ | ||
44 | if (WARN_ONCE(index > LONG_MAX || size > LONG_MAX, \ | ||
45 | "array_index_nospec() limited to range of [0, LONG_MAX]\n")) \ | ||
46 | _mask = 0; \ | ||
47 | else \ | ||
48 | _mask = array_index_mask_nospec(index, size); \ | ||
49 | _mask; \ | ||
50 | }) | ||
51 | |||
52 | /* | ||
47 | * array_index_nospec - sanitize an array index after a bounds check | 53 | * array_index_nospec - sanitize an array index after a bounds check |
48 | * | 54 | * |
49 | * For a code sequence like: | 55 | * For a code sequence like: |
@@ -61,7 +67,7 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, | |||
61 | ({ \ | 67 | ({ \ |
62 | typeof(index) _i = (index); \ | 68 | typeof(index) _i = (index); \ |
63 | typeof(size) _s = (size); \ | 69 | typeof(size) _s = (size); \ |
64 | unsigned long _mask = array_index_mask_nospec(_i, _s); \ | 70 | unsigned long _mask = array_index_mask_nospec_check(_i, _s); \ |
65 | \ | 71 | \ |
66 | BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \ | 72 | BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \ |
67 | BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \ | 73 | BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \ |