Commit babdde26 authored by Dan Williams's avatar Dan Williams Committed by Thomas Gleixner
Browse files

x86: Implement array_index_mask_nospec

array_index_nospec() uses a mask to sanitize user controllable array
indexes, i.e. generate a 0 mask if 'index' >= 'size', and a ~0 mask
otherwise. While the default array_index_mask_nospec() handles the
carry-bit from the (index - size) result in software.

The x86 array_index_mask_nospec() does the same, but the carry-bit is
handled in the processor CF flag without conditional instructions in the
control flow.

Suggested-by: default avatarLinus Torvalds <>
Signed-off-by: default avatarDan Williams <>
Signed-off-by: default avatarThomas Gleixner <>
parent f3804203
......@@ -24,6 +24,30 @@
#define wmb() asm volatile("sfence" ::: "memory")
* array_index_mask_nospec() - generate a mask that is ~0UL when the
* bounds check succeeds and 0 otherwise
* @index: array element index
* @size: number of elements in array
* Returns:
* 0 - (index < size)
static inline unsigned long array_index_mask_nospec(unsigned long index,
unsigned long size)
unsigned long mask;
asm ("cmp %1,%2; sbb %0,%0;"
:"=r" (mask)
:"r"(size),"r" (index)
return mask;
/* Override the default implementation from linux/nospec.h. */
#define array_index_mask_nospec array_index_mask_nospec
#define dma_rmb() rmb()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment