Skip to content

Commit 297f8ea

Browse files
wildea01gregkh
authored andcommitted
nospec: Move array_index_nospec() parameter checking into separate macro
commit 8fa80c5 upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent be1ea50 commit 297f8ea

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

include/linux/nospec.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@
1919
static inline unsigned long array_index_mask_nospec(unsigned long index,
2020
unsigned long size)
2121
{
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-
3622
/*
3723
* Always calculate and emit the mask even if the compiler
3824
* thinks the mask is not needed. The compiler does not take
@@ -43,6 +29,26 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
4329
}
4430
#endif
4531

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+
4652
/*
4753
* array_index_nospec - sanitize an array index after a bounds check
4854
*
@@ -61,7 +67,7 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
6167
({ \
6268
typeof(index) _i = (index); \
6369
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); \
6571
\
6672
BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \
6773
BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \

0 commit comments

Comments
 (0)