Message ID | 170839092792.398.3678407222202963581.tip-bot2@tip-bot2 |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp124726dyc; Mon, 19 Feb 2024 17:03:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVFz/KRYuLfu6iVVcZWkp964BYXE74yLHuNM6sHi5fF1h8PLhbAsVOCrw3fhVWESzoFiZ6tMg4wsgP0G/JixQSJV3RVHw== X-Google-Smtp-Source: AGHT+IHnXbWFVM2ZWXGPioSLoMqQhfNE7yi9BHOyWG5LYw35K64hRZ/3wblpQhFfyMs2VFcJy4MF X-Received: by 2002:a05:6402:718:b0:564:aada:4899 with SMTP id w24-20020a056402071800b00564aada4899mr1803395edx.4.1708391007421; Mon, 19 Feb 2024 17:03:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708391007; cv=pass; d=google.com; s=arc-20160816; b=fXSunTxSyIYrcvjO8LfT6nWhsqPFtdDBV3GMpn9hwqChLRYjBUgl1DWAS8WkU6MJfL sj7v56SjfUTnY7m0m6T42hEtEb/ksB6ABkPF2Se75Ar7jrgrJC/yW2y8+A86oa0H+IdA Y7VsoafxXcpHI8xcuzRQ6eRKrSbQCx45vcSXeCUhSsvXJJWq157au3aY0V3Qvw8i65U5 rrtEXRAsjSbt0sJYp8Fxc6+9TwVqNlz5bMOqpFWjQsYeVYmqdlYOcY7klQJNdxoO+4NL 6Ce9IfHHBzPuDhE6794oE+/P1dv0NUbUL0SUiI18CY4v8ChHs7zNrsuYzZbo2q3JXfqy IyFQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:cc:subject:to:reply-to:sender:from:dkim-signature :dkim-signature:date; bh=5HueHOvF9wc6rlDxHIpz6MwKLt9Wh96gKmMB8KfdNr4=; fh=YEnYTYGfZRPIomUE0pdpljehc1NDeSbRcikaQO1o7Tc=; b=0Z/nl82ikhvXsMWTzcfZ2jjJhmNgd7oMFUe5yjBwc6NbsjCURXJHFbbWii+55aSDDJ lukkEQJU53mORZXPUjIT2oxrpQgd3NxtkbkeCC5lwo7tZeBcPa1zDqruy7mnv1K8H+sI 6PdJyA3MEoppl8GqH6LsT6FyX5PUtRCl8/H39jgBkfb7n8KrZtEeqbsjFTEOFQLnV3Kw UhMxVujpcEiz1+RqkwMHEBJm56gNnDSk3SFLUyWfnJCrnulUe98uEHObvbUBegYtdn+W 5jEAX/2QysN7MkX8Mp0GM3a9vqGXW0jmzOQ9UVMoMIk7qQuBVm0n0gwEgnofOCtlo0g4 LzuQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="4ngIHsI/"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=0pQJiAja; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id s8-20020aa7d788000000b005648505032esi1028415edq.659.2024.02.19.17.03.27 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 17:03:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="4ngIHsI/"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=0pQJiAja; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72164-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DB46C1F22158 for <ouuuleilei@gmail.com>; Tue, 20 Feb 2024 01:03:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 272504CE00; Tue, 20 Feb 2024 01:02:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4ngIHsI/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0pQJiAja" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28FDE1E4B9; Tue, 20 Feb 2024 01:02:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708390933; cv=none; b=OIt6zsMFx6diXcjuPeGs+wsV9bmKdmqXdudtDRZUT1poaaB8vLlDo2QOGDnfnQKUzdsp43p/uybCNEzwAJcEQze/y+OiA4muB2GyVkBqTFIMzBV4Ab3lHeOLqWQtTEwgiwQ1tIHCa1VQTtNPgwi3KZc0mo85uHqwlfXQGPZNXv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708390933; c=relaxed/simple; bh=ItdoPLpGx6piTWvrxHODqmv30kLP3Gxe0J2Nz70hda4=; h=Date:From:To:Subject:Cc:MIME-Version:Message-ID:Content-Type; b=KF7YQvfoQdSq975ugxoEr7rZUGi9I97KugYuO68/3XjV1jK1qM5LyGNAaGIeNruiLfIL5lIppU1vlK/GJvbNS7cfRB7eM/PnVPEkHTV5rGdgxKWW8dMrn+HfVtFDv0EZVTGTO5quw5GNaww5aacrcNxBpKkuT+DUesk7Y5pPuDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4ngIHsI/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0pQJiAja; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Tue, 20 Feb 2024 01:02:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708390928; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5HueHOvF9wc6rlDxHIpz6MwKLt9Wh96gKmMB8KfdNr4=; b=4ngIHsI/V5+uA3+K0cMw8/knCeXjE+YlZDvJ5KjuuqBkWgEBwkxGUURmoQB7IqpD1dTi74 W/gxITVoembrV/3BANQbJ3XUBF+1pfO8z02QBVjdvlGg9wdqc2vL5CLmsc977uedxI1PkZ GbsuEZ6CxLvGr7+Wc/zQe8ZFZYgfKoCMUiY6lleMg0Ka2NX/M+zeL3Nfq204BIZDVNTrrG dvFTzbIaRfjaaQmk2Lp3YSYBstDA3UtB+Gj2LzqpLPSXK1lOOlLot1gNV1eg7axexE1xgf Gr5VJtcrb8iZj3/3UiiRxd4l6nKNFOS4YMw+CV2ot7iPSjd1JBjAlULX9e0KeA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708390928; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5HueHOvF9wc6rlDxHIpz6MwKLt9Wh96gKmMB8KfdNr4=; b=0pQJiAjaSWDEE7zktsf9/bMHpAiNqmm+qA8/uCIXfQ+VeKktnl71Be8Ki3IHdH7ZXQBqS3 vGozIiWoIJ7XV2Cw== From: "tip-bot2 for Pawan Gupta" <tip-bot2@linutronix.de> Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/bugs: Add asm helpers for executing VERW Cc: Alyssa Milburn <alyssa.milburn@intel.com>, Andrew Cooper <andrew.cooper3@citrix.com>, Peter Zijlstra <peterz@infradead.org>, Pawan Gupta <pawan.kumar.gupta@linux.intel.com>, Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org, linux-kernel@vger.kernel.org Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Message-ID: <170839092792.398.3678407222202963581.tip-bot2@tip-bot2> Robot-ID: <tip-bot2@linutronix.de> Robot-Unsubscribe: Contact <mailto:tglx@linutronix.de> to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791377809124880656 X-GMAIL-MSGID: 1791377809124880656 |
Series |
[tip:,x86/urgent] x86/bugs: Add asm helpers for executing VERW
|
|
Commit Message
tip-bot2 for Thomas Gleixner
Feb. 20, 2024, 1:02 a.m. UTC
The following commit has been merged into the x86/urgent branch of tip: Commit-ID: baf8361e54550a48a7087b603313ad013cc13386 Gitweb: https://git.kernel.org/tip/baf8361e54550a48a7087b603313ad013cc13386 Author: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> AuthorDate: Tue, 13 Feb 2024 18:21:35 -08:00 Committer: Dave Hansen <dave.hansen@linux.intel.com> CommitterDate: Mon, 19 Feb 2024 16:31:33 -08:00 x86/bugs: Add asm helpers for executing VERW MDS mitigation requires clearing the CPU buffers before returning to user. This needs to be done late in the exit-to-user path. Current location of VERW leaves a possibility of kernel data ending up in CPU buffers for memory accesses done after VERW such as: 1. Kernel data accessed by an NMI between VERW and return-to-user can remain in CPU buffers since NMI returning to kernel does not execute VERW to clear CPU buffers. 2. Alyssa reported that after VERW is executed, CONFIG_GCC_PLUGIN_STACKLEAK=y scrubs the stack used by a system call. Memory accesses during stack scrubbing can move kernel stack contents into CPU buffers. 3. When caller saved registers are restored after a return from function executing VERW, the kernel stack accesses can remain in CPU buffers(since they occur after VERW). To fix this VERW needs to be moved very late in exit-to-user path. In preparation for moving VERW to entry/exit asm code, create macros that can be used in asm. Also make VERW patching depend on a new feature flag X86_FEATURE_CLEAR_CPU_BUF. Reported-by: Alyssa Milburn <alyssa.milburn@intel.com> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com> Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/all/20240213-delay-verw-v8-1-a6216d83edb7%40linux.intel.com --- arch/x86/entry/entry.S | 23 +++++++++++++++++++++++ arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/nospec-branch.h | 13 +++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-)
Comments
On 20.02.24 г. 3:02 ч., tip-bot2 for Pawan Gupta wrote: > The following commit has been merged into the x86/urgent branch of tip: > > Commit-ID: baf8361e54550a48a7087b603313ad013cc13386 > Gitweb: https://git.kernel.org/tip/baf8361e54550a48a7087b603313ad013cc13386 > Author: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> > AuthorDate: Tue, 13 Feb 2024 18:21:35 -08:00 > Committer: Dave Hansen <dave.hansen@linux.intel.com> > CommitterDate: Mon, 19 Feb 2024 16:31:33 -08:00 > > x86/bugs: Add asm helpers for executing VERW > > MDS mitigation requires clearing the CPU buffers before returning to > user. This needs to be done late in the exit-to-user path. Current > location of VERW leaves a possibility of kernel data ending up in CPU > buffers for memory accesses done after VERW such as: > > 1. Kernel data accessed by an NMI between VERW and return-to-user can > remain in CPU buffers since NMI returning to kernel does not > execute VERW to clear CPU buffers. > 2. Alyssa reported that after VERW is executed, > CONFIG_GCC_PLUGIN_STACKLEAK=y scrubs the stack used by a system > call. Memory accesses during stack scrubbing can move kernel stack > contents into CPU buffers. > 3. When caller saved registers are restored after a return from > function executing VERW, the kernel stack accesses can remain in > CPU buffers(since they occur after VERW). > > To fix this VERW needs to be moved very late in exit-to-user path. > > In preparation for moving VERW to entry/exit asm code, create macros > that can be used in asm. Also make VERW patching depend on a new feature > flag X86_FEATURE_CLEAR_CPU_BUF. > > Reported-by: Alyssa Milburn <alyssa.milburn@intel.com> > Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com> > Suggested-by: Peter Zijlstra <peterz@infradead.org> > Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> > Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> > Link: https://lore.kernel.org/all/20240213-delay-verw-v8-1-a6216d83edb7%40linux.intel.com > --- > arch/x86/entry/entry.S | 23 +++++++++++++++++++++++ > arch/x86/include/asm/cpufeatures.h | 2 +- > arch/x86/include/asm/nospec-branch.h | 13 +++++++++++++ > 3 files changed, 37 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S > index 8c8d38f..0033790 100644 > --- a/arch/x86/entry/entry.S > +++ b/arch/x86/entry/entry.S > @@ -6,6 +6,9 @@ > #include <linux/export.h> > #include <linux/linkage.h> > #include <asm/msr-index.h> > +#include <asm/unwind_hints.h> > +#include <asm/segment.h> > +#include <asm/cache.h> > > .pushsection .noinstr.text, "ax" > > @@ -20,3 +23,23 @@ SYM_FUNC_END(entry_ibpb) > EXPORT_SYMBOL_GPL(entry_ibpb); > > .popsection > + > +/* > + * Define the VERW operand that is disguised as entry code so that > + * it can be referenced with KPTI enabled. This ensure VERW can be > + * used late in exit-to-user path after page tables are switched. > + */ > +.pushsection .entry.text, "ax" > + > +.align L1_CACHE_BYTES, 0xcc > +SYM_CODE_START_NOALIGN(mds_verw_sel) > + UNWIND_HINT_UNDEFINED > + ANNOTATE_NOENDBR > + .word __KERNEL_DS > +.align L1_CACHE_BYTES, 0xcc > +SYM_CODE_END(mds_verw_sel); > +/* For KVM */ > +EXPORT_SYMBOL_GPL(mds_verw_sel); > + > +.popsection > + > diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h > index fdf723b..2b62cdd 100644 > --- a/arch/x86/include/asm/cpufeatures.h > +++ b/arch/x86/include/asm/cpufeatures.h > @@ -95,7 +95,7 @@ > #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ > #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ > #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ > -/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ > +#define X86_FEATURE_CLEAR_CPU_BUF ( 3*32+18) /* "" Clear CPU buffers using VERW */ > #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ > #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ > #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index 262e655..077083e 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -315,6 +315,17 @@ > #endif > .endm > > +/* > + * Macro to execute VERW instruction that mitigate transient data sampling > + * attacks such as MDS. On affected systems a microcode update overloaded VERW > + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. > + * > + * Note: Only the memory operand variant of VERW clears the CPU buffers. > + */ > +.macro CLEAR_CPU_BUFFERS > + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF Any particular reason why this uses RIP-relative vs an absolute address mode? I know in our private exchange you said there is no significance but for example older kernels have a missing relocation support in alternatives. This of course can be worked around by slightly changing the logic of the macro which means different kernels will have slightly different macros. Relocation support landed in: 270a69c4485d7d07516d058bcc0473c90ee22185 (6.5) > +.endm > + > #else /* __ASSEMBLY__ */ > > #define ANNOTATE_RETPOLINE_SAFE \ > @@ -536,6 +547,8 @@ DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush); > > DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear); > > +extern u16 mds_verw_sel; > + > #include <asm/segment.h> > > /** >
On Mon, Feb 26, 2024 at 09:17:30AM +0200, Nikolay Borisov wrote: > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > > index 262e655..077083e 100644 > > --- a/arch/x86/include/asm/nospec-branch.h > > +++ b/arch/x86/include/asm/nospec-branch.h > > @@ -315,6 +315,17 @@ > > #endif > > .endm > > +/* > > + * Macro to execute VERW instruction that mitigate transient data sampling > > + * attacks such as MDS. On affected systems a microcode update overloaded VERW > > + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. > > + * > > + * Note: Only the memory operand variant of VERW clears the CPU buffers. > > + */ > > +.macro CLEAR_CPU_BUFFERS > > + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF > > Any particular reason why this uses RIP-relative vs an absolute address > mode? Early versions of the series had the VERW arg pointing to the macro itself, that is why relative addressing was used. That got changed in a later version with all VERW sites pointing to a single memory location. > I know in our private exchange you said there is no significance but > for example older kernels have a missing relocation support in alternatives. > This of course can be worked around by slightly changing the logic of the > macro which means different kernels will have slightly different macros. Do you anticipate a problem with that? If yes, I can send a patch to use fixed addressing in upstream as well.
On 27.02.24 г. 0:10 ч., Pawan Gupta wrote: > On Mon, Feb 26, 2024 at 09:17:30AM +0200, Nikolay Borisov wrote: >>> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h >>> index 262e655..077083e 100644 >>> --- a/arch/x86/include/asm/nospec-branch.h >>> +++ b/arch/x86/include/asm/nospec-branch.h >>> @@ -315,6 +315,17 @@ >>> #endif >>> .endm >>> +/* >>> + * Macro to execute VERW instruction that mitigate transient data sampling >>> + * attacks such as MDS. On affected systems a microcode update overloaded VERW >>> + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. >>> + * >>> + * Note: Only the memory operand variant of VERW clears the CPU buffers. >>> + */ >>> +.macro CLEAR_CPU_BUFFERS >>> + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF >> >> Any particular reason why this uses RIP-relative vs an absolute address >> mode? > > Early versions of the series had the VERW arg pointing to the macro > itself, that is why relative addressing was used. That got changed in a > later version with all VERW sites pointing to a single memory location. > >> I know in our private exchange you said there is no significance but >> for example older kernels have a missing relocation support in alternatives. >> This of course can be worked around by slightly changing the logic of the >> macro which means different kernels will have slightly different macros. > > Do you anticipate a problem with that? If yes, I can send a patch to use > fixed addressing in upstream as well. I experienced crashes on older kernels before realizing that the relocation wasn't resolved correctly by the alternative framework. Instead i simply changed the macro to jmp 1f, where the next instruction is the verw ( I did send a backport for 5.4) and it works. Recently there's been a push to make as much of the kernel assembly as possible PIC so having a rip-relative addressing helps. Whether that makes any material difference - I cannot say. Here's my backport version for reference: https://lore.kernel.org/stable/20240226122237.198921-3-nik.borisov@suse.com/
On Tue, Feb 27, 2024 at 12:20:03AM +0200, Nikolay Borisov wrote: > > > On 27.02.24 г. 0:10 ч., Pawan Gupta wrote: > > On Mon, Feb 26, 2024 at 09:17:30AM +0200, Nikolay Borisov wrote: > > > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > > > > index 262e655..077083e 100644 > > > > --- a/arch/x86/include/asm/nospec-branch.h > > > > +++ b/arch/x86/include/asm/nospec-branch.h > > > > @@ -315,6 +315,17 @@ > > > > #endif > > > > .endm > > > > +/* > > > > + * Macro to execute VERW instruction that mitigate transient data sampling > > > > + * attacks such as MDS. On affected systems a microcode update overloaded VERW > > > > + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. > > > > + * > > > > + * Note: Only the memory operand variant of VERW clears the CPU buffers. > > > > + */ > > > > +.macro CLEAR_CPU_BUFFERS > > > > + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF > > > > > > Any particular reason why this uses RIP-relative vs an absolute address > > > mode? > > > > Early versions of the series had the VERW arg pointing to the macro > > itself, that is why relative addressing was used. That got changed in a > > later version with all VERW sites pointing to a single memory location. > > > > > I know in our private exchange you said there is no significance but > > > for example older kernels have a missing relocation support in alternatives. > > > This of course can be worked around by slightly changing the logic of the > > > macro which means different kernels will have slightly different macros. > > > > Do you anticipate a problem with that? If yes, I can send a patch to use > > fixed addressing in upstream as well. > > I experienced crashes on older kernels before realizing that the relocation > wasn't resolved correctly by the alternative framework. Instead i simply > changed the macro to jmp 1f, where the next instruction is the verw ( I did > send a backport for 5.4) and it works. Recently there's been a push to make > as much of the kernel assembly as possible PIC so having a rip-relative > addressing helps. Whether that makes any material difference - I cannot say. Ok, sending the patch. > Here's my backport version for reference: > > https://lore.kernel.org/stable/20240226122237.198921-3-nik.borisov@suse.com/ Below should also solve the problem with less churn: --- diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 2aa52cab1e46..ab19c7f1167b 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -323,7 +323,7 @@ * Note: Only the memory operand variant of VERW clears the CPU buffers. */ .macro CLEAR_CPU_BUFFERS - ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", __stringify(verw mds_verw_sel), X86_FEATURE_CLEAR_CPU_BUF .endm #else /* __ASSEMBLY__ */
diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S index 8c8d38f..0033790 100644 --- a/arch/x86/entry/entry.S +++ b/arch/x86/entry/entry.S @@ -6,6 +6,9 @@ #include <linux/export.h> #include <linux/linkage.h> #include <asm/msr-index.h> +#include <asm/unwind_hints.h> +#include <asm/segment.h> +#include <asm/cache.h> .pushsection .noinstr.text, "ax" @@ -20,3 +23,23 @@ SYM_FUNC_END(entry_ibpb) EXPORT_SYMBOL_GPL(entry_ibpb); .popsection + +/* + * Define the VERW operand that is disguised as entry code so that + * it can be referenced with KPTI enabled. This ensure VERW can be + * used late in exit-to-user path after page tables are switched. + */ +.pushsection .entry.text, "ax" + +.align L1_CACHE_BYTES, 0xcc +SYM_CODE_START_NOALIGN(mds_verw_sel) + UNWIND_HINT_UNDEFINED + ANNOTATE_NOENDBR + .word __KERNEL_DS +.align L1_CACHE_BYTES, 0xcc +SYM_CODE_END(mds_verw_sel); +/* For KVM */ +EXPORT_SYMBOL_GPL(mds_verw_sel); + +.popsection + diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index fdf723b..2b62cdd 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -95,7 +95,7 @@ #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ -/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ +#define X86_FEATURE_CLEAR_CPU_BUF ( 3*32+18) /* "" Clear CPU buffers using VERW */ #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 262e655..077083e 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -315,6 +315,17 @@ #endif .endm +/* + * Macro to execute VERW instruction that mitigate transient data sampling + * attacks such as MDS. On affected systems a microcode update overloaded VERW + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. + * + * Note: Only the memory operand variant of VERW clears the CPU buffers. + */ +.macro CLEAR_CPU_BUFFERS + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF +.endm + #else /* __ASSEMBLY__ */ #define ANNOTATE_RETPOLINE_SAFE \ @@ -536,6 +547,8 @@ DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush); DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear); +extern u16 mds_verw_sel; + #include <asm/segment.h> /**