[PATCH 2/3] ARM: spectre-v1: add array_index_mask_nospec() implementation
From: mark.rutland@arm.com (Mark Rutland)
Date: 2018-05-31 14:22:05
On Thu, May 31, 2018 at 02:31:17PM +0100, Russell King wrote:
quoted hunk ↗ jump to hunk
Add an implementation of the array_index_mask_nospec() function for mitigating Spectre variant 1 throughout the kernel. Signed-off-by: Russell King <redacted> --- arch/arm/include/asm/barrier.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)diff --git a/arch/arm/include/asm/barrier.h b/arch/arm/include/asm/barrier.h index 3d9c1d4b7e75..a89adcf53b99 100644 --- a/arch/arm/include/asm/barrier.h +++ b/arch/arm/include/asm/barrier.h@@ -76,6 +76,24 @@ extern void arm_heavy_mb(void); #define __smp_rmb() __smp_mb() #define __smp_wmb() dmb(ishst) +#ifdef CONFIG_CPU_SPECTRE +static inline unsigned long array_index_mask_nospec(unsigned long idx, + unsigned long sz) +{ + unsigned long mask; + + asm( "cmp %1, %2\n" + " sbc %0, %1, %1\n" + CSDB + : "=r" (mask) + : "r" (idx), "Ir" (sz) + : "cc"); + + return mask; +} +#define array_index_mask_nospec array_index_mask_nospec +#endif
I believe that the asm should be volatile, otherwise there are cases where the compiler could remove an invocation that's redundant under architectural execution, but required under speculation [1]. With that made volatile: Acked-by: Mark Rutland <mark.rutland@arm.com> Thanks, Mark. [1] https://lkml.kernel.org/r/20180530060546.jnjoltsturoduohh at salmiak