Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 21 Jan 2019 19:54:32 -0000 Received: from mga11.intel.com ([192.55.52.93]) by Galois.linutronix.de with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1gkeqX-0003N5-6F for speck@linutronix.de; Sat, 19 Jan 2019 01:50:49 +0100 From: Andi Kleen Subject: [MODERATED] [PATCH v5 11/27] MDSv5 2 Date: Fri, 18 Jan 2019 16:50:26 -0800 Message-Id: <16925ccbcdde1d086ca5ed03351a033280a1ee2f.1547858934.git.ak@linux.intel.com> In-Reply-To: References: In-Reply-To: References: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 To: speck@linutronix.de Cc: Andi Kleen List-ID: Add basic infrastructure for code to request CPU buffer clearing on the next kernel exit. We have two functions lazy_clear_cpu to request clearing, and lazy_clear_cpu_interrupt to request clearing if running in an interrupt. Non architecture specific code can include linux/clearcpu.h and use lazy_clear_cpu / lazy_clear_interrupt. On x86 we provide low level implementations that set the TIF_CLEAR_CPU bit. Signed-off-by: Andi Kleen --- arch/Kconfig | 3 +++ arch/x86/Kconfig | 1 + arch/x86/include/asm/clearcpu.h | 5 +++++ include/linux/clearcpu.h | 36 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 include/linux/clearcpu.h diff --git a/arch/Kconfig b/arch/Kconfig index 4cfb6de48f79..e6b7bf9174aa 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -808,6 +808,9 @@ config VMAP_STACK the stack to map directly to the KASAN shadow map using a formula that is incorrect if the stack is in vmalloc space. +config ARCH_HAS_CLEAR_CPU + def_bool n + config ARCH_OPTIONAL_KERNEL_RWX def_bool n diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 15af091611e2..ee05fe6821eb 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -84,6 +84,7 @@ config X86 select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH select ARCH_WANTS_DYNAMIC_TASK_STRUCT select ARCH_WANTS_THP_SWAP if X86_64 + select ARCH_HAS_CLEAR_CPU select BUILDTIME_EXTABLE_SORT select CLKEVT_I8253 select CLOCKSOURCE_VALIDATE_LAST_CYCLE diff --git a/arch/x86/include/asm/clearcpu.h b/arch/x86/include/asm/clearcpu.h index dc3d04da6779..3be0194f48dc 100644 --- a/arch/x86/include/asm/clearcpu.h +++ b/arch/x86/include/asm/clearcpu.h @@ -40,6 +40,11 @@ static inline void clear_cpu_idle(void) } } +static inline void lazy_clear_cpu(void) +{ + set_thread_flag(TIF_CLEAR_CPU); +} + DECLARE_STATIC_KEY_FALSE(force_cpu_clear); #endif diff --git a/include/linux/clearcpu.h b/include/linux/clearcpu.h new file mode 100644 index 000000000000..63a6952b46fa --- /dev/null +++ b/include/linux/clearcpu.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_CLEARCPU_H +#define _LINUX_CLEARCPU_H 1 + +#include + +#ifdef CONFIG_ARCH_HAS_CLEAR_CPU +#include +#else +static inline void lazy_clear_cpu(void) +{ +} +#endif + +/* + * Use this function when potentially touching (reading or writing) + * user data in an interrupt. In this case schedule to clear the + * CPU buffers on kernel exit to avoid any potential side channels. + * + * If not in an interrupt we assume the touched data belongs to the + * current process and doesn't need to be cleared. + * + * This version is for code who might be in an interrupt. + * If you know for sure you're in interrupt context call + * lazy_clear_cpu directly. + * + * lazy_clear_cpu is reasonably cheap (just sets a bit) and + * can be used in fast paths. + */ +static inline void lazy_clear_cpu_interrupt(void) +{ + if (in_interrupt()) + lazy_clear_cpu(); +} + +#endif -- 2.17.2