linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/16] Enable Linear Address Space Separation support
@ 2024-07-10 16:06 Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits Alexander Shishkin
                   ` (15 more replies)
  0 siblings, 16 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

Changes from v3[6]:
- Made LAM dependent on LASS
- Moved EFI runtime initialization to x86 side of things
- Suspended LASS validation around EFI set_virtual_address_map call
- Added a message for the case of kernel side LASS violation
- Moved inline memset/memcpy versions to the common string.h

Changes from v2[5]:
- Added myself to the SoB chain

Changes from v1[1]:
- Emulate vsyscall violations in execute mode in the #GP fault handler
- Use inline memcpy and memset while patching alternatives
- Remove CONFIG_X86_LASS
- Make LASS depend on SMAP
- Dropped the minimal KVM enabling patch

Linear Address Space Separation (LASS) is a security feature that intends to
prevent malicious virtual address space accesses across user/kernel mode.

Such mode based access protection already exists today with paging and features
such as SMEP and SMAP. However, to enforce these protections, the processor
must traverse the paging structures in memory.  Malicious software can use
timing information resulting from this traversal to determine details about the
paging structures, and these details may also be used to determine the layout
of the kernel memory.

The LASS mechanism provides the same mode-based protections as paging but
without traversing the paging structures. Because the protections enforced by
LASS are applied before paging, software will not be able to derive
paging-based timing information from the various caching structures such as the
TLBs, mid-level caches, page walker, data caches, etc. LASS can avoid probing
using double page faults, TLB flush and reload, and SW prefetch instructions.
See [2], [3] and [4] for some research on the related attack vectors.

In addition, LASS prevents an attack vector described in a Spectre LAM (SLAM)
whitepaper [7].

LASS enforcement relies on the typical kernel implemetation to divide the
64-bit virtual address space into two halves:
  Addr[63]=0 -> User address space
  Addr[63]=1 -> Kernel address space
Any data access or code execution across address spaces typically results in a
#GP fault.

Kernel accesses usually only happen to the kernel address space. However, there
are valid reasons for kernel to access memory in the user half. For these cases
(such as text poking and EFI runtime accesses), the kernel can temporarily
suspend the enforcement of LASS by toggling SMAP (Supervisor Mode Access
Prevention) using the stac()/clac() instructions and in one instance a downright
disabling LASS for an EFI runtime call.

User space cannot access any kernel address while LASS is enabled.
Unfortunately, legacy vsyscall functions are located in the address range
0xffffffffff600000 - 0xffffffffff601000 and emulated in kernel.  To avoid
breaking user applications when LASS is enabled, extend the vsyscall emulation
in execute (XONLY) mode to the #GP fault handler.

In contrast, the vsyscall EMULATE mode is deprecated and not expected to be
used by anyone.  Supporting EMULATE mode with LASS would need complex
intruction decoding in the #GP fault handler and is probably not worth the
hassle. Disable LASS in this rare case when someone absolutely needs and
enables vsyscall=emulate via the command line.

[1] https://lore.kernel.org/lkml/20230110055204.3227669-1-yian.chen@intel.com/
[2] “Practical Timing Side Channel Attacks against Kernel Space ASLR”,
https://www.ieee-security.org/TC/SP2013/papers/4977a191.pdf
[3] “Prefetch Side-Channel Attacks: Bypassing SMAP and Kernel ASLR”, http://doi.acm.org/10.1145/2976749.2978356
[4] “Harmful prefetch on Intel”, https://ioactive.com/harmful-prefetch-on-intel/ (H/T Anders)
[5] https://lore.kernel.org/all/20230530114247.21821-1-alexander.shishkin@linux.intel.com/
[6] https://lore.kernel.org/all/20230609183632.48706-1-alexander.shishkin@linux.intel.com/
[7] https://download.vusec.net/papers/slam_sp24.pdf


Alexander Shishkin (6):
  init/main.c: Move EFI runtime service initialization to x86/cpu
  x86/cpu: Defer CR pinning setup until after EFI initialization
  x86/vsyscall: Document the fact that vsyscall=emulate disables LASS
  x86/traps: Communicate a LASS violation in #GP message
  efi: Disable LASS around set_virtual_address_map call
  x86/cpu: Make LAM depend on LASS

Peter Zijlstra (1):
  x86/asm: Introduce inline memcpy and memset

Sohil Mehta (8):
  x86/cpu: Enumerate the LASS feature bits
  x86/alternatives: Disable LASS when patching kernel alternatives
  x86/cpu: Enable LASS during CPU initialization
  x86/cpu: Remove redundant comment during feature setup
  x86/vsyscall: Reorganize the #PF emulation code
  x86/traps: Consolidate user fixups in exc_general_protection()
  x86/vsyscall: Add vsyscall emulation for #GP
  x86/vsyscall: Disable LASS if vsyscall mode is set to EMULATE

Yian Chen (1):
  x86/cpu: Set LASS CR4 bit as pinning sensitive

 .../admin-guide/kernel-parameters.txt         |  4 +-
 arch/x86/entry/vsyscall/vsyscall_64.c         | 61 +++++++++++++------
 arch/x86/include/asm/cpufeatures.h            |  1 +
 arch/x86/include/asm/disabled-features.h      |  4 +-
 arch/x86/include/asm/smap.h                   |  4 ++
 arch/x86/include/asm/string.h                 | 26 ++++++++
 arch/x86/include/asm/vsyscall.h               | 14 +++--
 arch/x86/include/uapi/asm/processor-flags.h   |  2 +
 arch/x86/kernel/alternative.c                 | 12 +++-
 arch/x86/kernel/cpu/common.c                  | 25 +++++++-
 arch/x86/kernel/cpu/cpuid-deps.c              |  2 +
 arch/x86/kernel/traps.c                       | 26 +++++---
 arch/x86/mm/fault.c                           |  2 +-
 arch/x86/platform/efi/efi.c                   | 13 ++++
 init/main.c                                   |  5 --
 tools/arch/x86/include/asm/cpufeatures.h      |  1 +
 16 files changed, 157 insertions(+), 45 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2025-05-13  3:08   ` Xin Li
  2024-07-10 16:06 ` [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset Alexander Shishkin
                   ` (14 subsequent siblings)
  15 siblings, 1 reply; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

Linear Address Space Separation (LASS) is a security feature that
intends to prevent malicious virtual address space accesses across
user/kernel mode.

Such mode based access protection already exists today with paging and
features such as SMEP and SMAP. However, to enforce these protections,
the processor must traverse the paging structures in memory.  Malicious
software can use timing information resulting from this traversal to
determine details about the paging structures, and these details may
also be used to determine the layout of the kernel memory.

The LASS mechanism provides the same mode-based protections as paging
but without traversing the paging structures. Because the protections
enforced by LASS are applied before paging, software will not be able to
derive paging-based timing information from the various caching
structures such as the TLBs, mid-level caches, page walker, data caches,
etc.

LASS enforcement relies on the typical kernel implementation to divide
the 64-bit virtual address space into two halves:
  Addr[63]=0 -> User address space
  Addr[63]=1 -> Kernel address space

Any data access or code execution across address spaces typically
results in a #GP fault.

The LASS enforcement for kernel data access is dependent on CR4.SMAP
being set. The enforcement can be disabled by toggling the RFLAGS.AC bit
similar to SMAP.

Define the CPU feature bits to enumerate this feature and include
feature dependencies to reflect the same.

Co-developed-by: Yian Chen <yian.chen@intel.com>
Signed-off-by: Yian Chen <yian.chen@intel.com>
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/include/asm/cpufeatures.h          | 1 +
 arch/x86/include/asm/disabled-features.h    | 4 +++-
 arch/x86/include/asm/smap.h                 | 4 ++++
 arch/x86/include/uapi/asm/processor-flags.h | 2 ++
 arch/x86/kernel/cpu/cpuid-deps.c            | 1 +
 tools/arch/x86/include/asm/cpufeatures.h    | 1 +
 6 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
index 3c7434329661..874809e4547c 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -319,6 +319,7 @@
 /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
 #define X86_FEATURE_AVX_VNNI		(12*32+ 4) /* AVX VNNI instructions */
 #define X86_FEATURE_AVX512_BF16		(12*32+ 5) /* AVX512 BFLOAT16 instructions */
+#define X86_FEATURE_LASS		(12*32+ 6) /* Linear Address Space Separation */
 #define X86_FEATURE_CMPCCXADD           (12*32+ 7) /* "" CMPccXADD instructions */
 #define X86_FEATURE_ARCH_PERFMON_EXT	(12*32+ 8) /* "" Intel Architectural PerfMon Extension */
 #define X86_FEATURE_FZRM		(12*32+10) /* "" Fast zero-length REP MOVSB */
diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h
index c492bdc97b05..76c7d362af94 100644
--- a/arch/x86/include/asm/disabled-features.h
+++ b/arch/x86/include/asm/disabled-features.h
@@ -22,12 +22,14 @@
 # define DISABLE_CYRIX_ARR	(1<<(X86_FEATURE_CYRIX_ARR & 31))
 # define DISABLE_CENTAUR_MCR	(1<<(X86_FEATURE_CENTAUR_MCR & 31))
 # define DISABLE_PCID		0
+# define DISABLE_LASS		0
 #else
 # define DISABLE_VME		0
 # define DISABLE_K6_MTRR	0
 # define DISABLE_CYRIX_ARR	0
 # define DISABLE_CENTAUR_MCR	0
 # define DISABLE_PCID		(1<<(X86_FEATURE_PCID & 31))
+# define DISABLE_LASS		(1<<(X86_FEATURE_LASS & 31))
 #endif /* CONFIG_X86_64 */
 
 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
@@ -146,7 +148,7 @@
 #define DISABLED_MASK11	(DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \
 			 DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK)
 #define DISABLED_MASK12	(DISABLE_FRED|DISABLE_LAM)
-#define DISABLED_MASK13	0
+#define DISABLED_MASK13	(DISABLE_LASS)
 #define DISABLED_MASK14	0
 #define DISABLED_MASK15	0
 #define DISABLED_MASK16	(DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \
diff --git a/arch/x86/include/asm/smap.h b/arch/x86/include/asm/smap.h
index bab490379c65..776dce849a58 100644
--- a/arch/x86/include/asm/smap.h
+++ b/arch/x86/include/asm/smap.h
@@ -27,6 +27,10 @@
 
 #else /* __ASSEMBLY__ */
 
+/*
+ * The CLAC/STAC instructions toggle enforcement of X86_FEATURE_SMAP as well as
+ * X86_FEATURE_LASS.
+ */
 static __always_inline void clac(void)
 {
 	/* Note: a barrier is implicit in alternative() */
diff --git a/arch/x86/include/uapi/asm/processor-flags.h b/arch/x86/include/uapi/asm/processor-flags.h
index f1a4adc78272..81d0c8bf1137 100644
--- a/arch/x86/include/uapi/asm/processor-flags.h
+++ b/arch/x86/include/uapi/asm/processor-flags.h
@@ -136,6 +136,8 @@
 #define X86_CR4_PKE		_BITUL(X86_CR4_PKE_BIT)
 #define X86_CR4_CET_BIT		23 /* enable Control-flow Enforcement Technology */
 #define X86_CR4_CET		_BITUL(X86_CR4_CET_BIT)
+#define X86_CR4_LASS_BIT	27 /* enable Linear Address Space Separation support */
+#define X86_CR4_LASS		_BITUL(X86_CR4_LASS_BIT)
 #define X86_CR4_LAM_SUP_BIT	28 /* LAM for supervisor pointers */
 #define X86_CR4_LAM_SUP		_BITUL(X86_CR4_LAM_SUP_BIT)
 
diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index b7d9f530ae16..22612e01ec2e 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = {
 	{ X86_FEATURE_SHSTK,			X86_FEATURE_XSAVES    },
 	{ X86_FEATURE_FRED,			X86_FEATURE_LKGS      },
 	{ X86_FEATURE_FRED,			X86_FEATURE_WRMSRNS   },
+	{ X86_FEATURE_LASS,			X86_FEATURE_SMAP      },
 	{}
 };
 
diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h
index 3c7434329661..874809e4547c 100644
--- a/tools/arch/x86/include/asm/cpufeatures.h
+++ b/tools/arch/x86/include/asm/cpufeatures.h
@@ -319,6 +319,7 @@
 /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
 #define X86_FEATURE_AVX_VNNI		(12*32+ 4) /* AVX VNNI instructions */
 #define X86_FEATURE_AVX512_BF16		(12*32+ 5) /* AVX512 BFLOAT16 instructions */
+#define X86_FEATURE_LASS		(12*32+ 6) /* Linear Address Space Separation */
 #define X86_FEATURE_CMPCCXADD           (12*32+ 7) /* "" CMPccXADD instructions */
 #define X86_FEATURE_ARCH_PERFMON_EXT	(12*32+ 8) /* "" Intel Architectural PerfMon Extension */
 #define X86_FEATURE_FZRM		(12*32+10) /* "" Fast zero-length REP MOVSB */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-11  8:13   ` Peter Zijlstra
  2024-07-10 16:06 ` [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives Alexander Shishkin
                   ` (13 subsequent siblings)
  15 siblings, 1 reply; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Peter Zijlstra <peterz@infradead.org>

Provide inline memcpy and memset functions that can be used instead of
the GCC builtins whenever necessary.

Code posted by Peter Zijlstra <peterz@infradead.org>.
Link: https://lore.kernel.org/lkml/Y759AJ%2F0N9fqwDED@hirez.programming.kicks-ass.net/
[Missing Signed-off-by from PeterZ]
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/include/asm/string.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/arch/x86/include/asm/string.h b/arch/x86/include/asm/string.h
index c3c2c1914d65..9cb5aae7fba9 100644
--- a/arch/x86/include/asm/string.h
+++ b/arch/x86/include/asm/string.h
@@ -1,6 +1,32 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_STRING_H
+#define _ASM_X86_STRING_H
+
 #ifdef CONFIG_X86_32
 # include <asm/string_32.h>
 #else
 # include <asm/string_64.h>
 #endif
+
+static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
+{
+	void *ret = to;
+
+	asm volatile("rep movsb"
+		     : "+D" (to), "+S" (from), "+c" (len)
+		     : : "memory");
+	return ret;
+}
+
+static __always_inline void *__inline_memset(void *s, int v, size_t n)
+{
+	void *ret = s;
+
+	asm volatile("rep stosb"
+		     : "+D" (s), "+c" (n)
+		     : "a" ((uint8_t)v)
+		     : "memory");
+	return ret;
+}
+
+#endif /* _ASM_X86_STRING_H */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 17:18   ` Borislav Petkov
  2024-07-10 16:06 ` [PATCH v4 04/16] x86/cpu: Enable LASS during CPU initialization Alexander Shishkin
                   ` (12 subsequent siblings)
  15 siblings, 1 reply; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

For patching, the kernel initializes a temporary mm area in the lower
half of the address range. See commit 4fc19708b165 ("x86/alternatives:
Initialize temporary mm for patching").

Disable LASS enforcement during patching using the stac()/clac()
instructions to avoid triggering a #GP fault.

The objtool warns due to a call to a non-allowed function that exists
outside of the stac/clac guard, or references to any function with a
dynamic function pointer inside the guard. See the Objtool warnings
section #9 in the document tools/objtool/Documentation/objtool.txt.

Considering that patching is usually small, replace the memcpy and
memset functions in the text poking functions with their inline versions
respectively.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/alternative.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 89de61243272..c6e1b17d1da1 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -1825,16 +1825,24 @@ static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
 __ro_after_init struct mm_struct *poking_mm;
 __ro_after_init unsigned long poking_addr;
 
+/*
+ * poking_init() initializes the text poking address from the lower half of the
+ * address space. Relax LASS enforcement when accessing the poking address.
+ */
 static void text_poke_memcpy(void *dst, const void *src, size_t len)
 {
-	memcpy(dst, src, len);
+	stac();
+	__inline_memcpy(dst, src, len);
+	clac();
 }
 
 static void text_poke_memset(void *dst, const void *src, size_t len)
 {
 	int c = *(const int *)src;
 
-	memset(dst, c, len);
+	stac();
+	__inline_memset(dst, c, len);
+	clac();
 }
 
 typedef void text_poke_f(void *dst, const void *src, size_t len);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 04/16] x86/cpu: Enable LASS during CPU initialization
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (2 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup Alexander Shishkin
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

Being a security feature, enable LASS by default if the platform
supports it.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/cpu/common.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index d4e539d4e158..dcf61a66e462 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -398,6 +398,12 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)
 	cr4_clear_bits(X86_CR4_UMIP);
 }
 
+static __always_inline void setup_lass(struct cpuinfo_x86 *c)
+{
+	if (cpu_feature_enabled(X86_FEATURE_LASS))
+		cr4_set_bits(X86_CR4_LASS);
+}
+
 /* These bits should not change their value after CPU init is finished. */
 static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
 					     X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED;
@@ -1839,6 +1845,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	setup_smep(c);
 	setup_smap(c);
 	setup_umip(c);
+	setup_lass(c);
 
 	/* Enable FSGSBASE instructions if available. */
 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (3 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 04/16] x86/cpu: Enable LASS during CPU initialization Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 17:21   ` Borislav Petkov
  2024-07-10 16:06 ` [PATCH v4 06/16] init/main.c: Move EFI runtime service initialization to x86/cpu Alexander Shishkin
                   ` (10 subsequent siblings)
  15 siblings, 1 reply; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

The code below the comment is self explanatory. Instead of updating the
comment with the newly added LASS feature, it is better to just remove
it.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/cpu/common.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index dcf61a66e462..33a76256a6f5 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1841,7 +1841,6 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	/* Disable the PN if appropriate */
 	squash_the_stupid_serial_number(c);
 
-	/* Set up SMEP/SMAP/UMIP */
 	setup_smep(c);
 	setup_smap(c);
 	setup_umip(c);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 06/16] init/main.c: Move EFI runtime service initialization to x86/cpu
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (4 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization Alexander Shishkin
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

The EFI call in start_kernel() is guarded by #ifdef CONFIG_X86. Move
the thing to the arch_cpu_finalize_init() path on x86 and get rid of
the #ifdef in start_kernel().

No functional change intended.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Suggested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 arch/x86/kernel/cpu/common.c | 7 +++++++
 init/main.c                  | 5 -----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 33a76256a6f5..8aa621dc7d30 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -26,6 +26,7 @@
 #include <linux/pgtable.h>
 #include <linux/stackprotector.h>
 #include <linux/utsname.h>
+#include <linux/efi.h>
 
 #include <asm/alternative.h>
 #include <asm/cmdline.h>
@@ -2364,6 +2365,12 @@ void __init arch_cpu_finalize_init(void)
 	fpu__init_system();
 	fpu__init_cpu();
 
+	/*
+	 * This needs to follow the FPU initializtion, since EFI depends on it.
+	 */
+	if (efi_enabled(EFI_RUNTIME_SERVICES))
+		efi_enter_virtual_mode();
+
 	/*
 	 * Ensure that access to the per CPU representation has the initial
 	 * boot CPU configuration.
diff --git a/init/main.c b/init/main.c
index 206acdde51f5..cce4ceaf7c9c 100644
--- a/init/main.c
+++ b/init/main.c
@@ -51,7 +51,6 @@
 #include <linux/cpu.h>
 #include <linux/cpuset.h>
 #include <linux/cgroup.h>
-#include <linux/efi.h>
 #include <linux/tick.h>
 #include <linux/sched/isolation.h>
 #include <linux/interrupt.h>
@@ -1070,10 +1069,6 @@ void start_kernel(void)
 
 	pid_idr_init();
 	anon_vma_init();
-#ifdef CONFIG_X86
-	if (efi_enabled(EFI_RUNTIME_SERVICES))
-		efi_enter_virtual_mode();
-#endif
 	thread_stack_cache_init();
 	cred_init();
 	fork_init();
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (5 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 06/16] init/main.c: Move EFI runtime service initialization to x86/cpu Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-11  8:11   ` Peter Zijlstra
  2024-07-10 16:06 ` [PATCH v4 08/16] x86/vsyscall: Reorganize the #PF emulation code Alexander Shishkin
                   ` (8 subsequent siblings)
  15 siblings, 1 reply; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

In order to map the EFI runtime services, set_virtual_address_map
needs to be called, which resides in the lower half of the address
space. This means that LASS needs to be temporarily disabled around
this call. This can only be done before the CR pinning is set up.

Move CR pinning setup behind the EFI initialization.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Suggested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 arch/x86/kernel/cpu/common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 8aa621dc7d30..c93c59a27dfa 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1948,7 +1948,6 @@ static __init void identify_boot_cpu(void)
 	enable_sep_cpu();
 #endif
 	cpu_detect_tlb(&boot_cpu_data);
-	setup_cr_pinning();
 
 	tsx_init();
 	tdx_init();
@@ -2367,10 +2366,16 @@ void __init arch_cpu_finalize_init(void)
 
 	/*
 	 * This needs to follow the FPU initializtion, since EFI depends on it.
+	 * It also needs to precede the CR pinning setup, because we need to be
+	 * able to temporarily clear the CR4.LASS bit in order to execute the
+	 * set_virtual_address_map call, which resides in lower addresses and
+	 * would trip LASS if enabled.
 	 */
 	if (efi_enabled(EFI_RUNTIME_SERVICES))
 		efi_enter_virtual_mode();
 
+	setup_cr_pinning();
+
 	/*
 	 * Ensure that access to the per CPU representation has the initial
 	 * boot CPU configuration.
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 08/16] x86/vsyscall: Reorganize the #PF emulation code
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (6 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 09/16] x86/traps: Consolidate user fixups in exc_general_protection() Alexander Shishkin
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

Separate out the actual vsyscall emulation from the page fault specific
handling in preparation for the upcoming #GP fault emulation.

No functional change intended.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/entry/vsyscall/vsyscall_64.c | 42 +++++++++++++++------------
 arch/x86/include/asm/vsyscall.h       |  8 ++---
 arch/x86/mm/fault.c                   |  2 +-
 3 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
index 2fb7d53cf333..e89d7d83a594 100644
--- a/arch/x86/entry/vsyscall/vsyscall_64.c
+++ b/arch/x86/entry/vsyscall/vsyscall_64.c
@@ -112,30 +112,13 @@ static bool write_ok_or_segv(unsigned long ptr, size_t size)
 	}
 }
 
-bool emulate_vsyscall(unsigned long error_code,
-		      struct pt_regs *regs, unsigned long address)
+static bool __emulate_vsyscall(struct pt_regs *regs, unsigned long address)
 {
 	unsigned long caller;
 	int vsyscall_nr, syscall_nr, tmp;
 	long ret;
 	unsigned long orig_dx;
 
-	/* Write faults or kernel-privilege faults never get fixed up. */
-	if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)
-		return false;
-
-	if (!(error_code & X86_PF_INSTR)) {
-		/* Failed vsyscall read */
-		if (vsyscall_mode == EMULATE)
-			return false;
-
-		/*
-		 * User code tried and failed to read the vsyscall page.
-		 */
-		warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");
-		return false;
-	}
-
 	/*
 	 * No point in checking CS -- the only way to get here is a user mode
 	 * trap to a high address, which means that we're in 64-bit user code.
@@ -270,6 +253,29 @@ bool emulate_vsyscall(unsigned long error_code,
 	return true;
 }
 
+bool emulate_vsyscall_pf(unsigned long error_code, struct pt_regs *regs,
+			 unsigned long address)
+{
+	/* Write faults or kernel-privilege faults never get fixed up. */
+	if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)
+		return false;
+
+	if (!(error_code & X86_PF_INSTR)) {
+		/* Failed vsyscall read */
+		if (vsyscall_mode == EMULATE)
+			return false;
+
+		/*
+		 * User code tried and failed to read the vsyscall page.
+		 */
+		warn_bad_vsyscall(KERN_INFO, regs,
+				  "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");
+		return false;
+	}
+
+	return __emulate_vsyscall(regs, address);
+}
+
 /*
  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
diff --git a/arch/x86/include/asm/vsyscall.h b/arch/x86/include/asm/vsyscall.h
index 472f0263dbc6..214977f4fa11 100644
--- a/arch/x86/include/asm/vsyscall.h
+++ b/arch/x86/include/asm/vsyscall.h
@@ -14,12 +14,12 @@ extern void set_vsyscall_pgtable_user_bits(pgd_t *root);
  * Called on instruction fetch fault in vsyscall page.
  * Returns true if handled.
  */
-extern bool emulate_vsyscall(unsigned long error_code,
-			     struct pt_regs *regs, unsigned long address);
+extern bool emulate_vsyscall_pf(unsigned long error_code,
+				struct pt_regs *regs, unsigned long address);
 #else
 static inline void map_vsyscall(void) {}
-static inline bool emulate_vsyscall(unsigned long error_code,
-				    struct pt_regs *regs, unsigned long address)
+static inline bool emulate_vsyscall_pf(unsigned long error_code,
+				       struct pt_regs *regs, unsigned long address)
 {
 	return false;
 }
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index e6c469b323cc..44e2d1ef4128 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -1318,7 +1318,7 @@ void do_user_addr_fault(struct pt_regs *regs,
 	 * to consider the PF_PK bit.
 	 */
 	if (is_vsyscall_vaddr(address)) {
-		if (emulate_vsyscall(error_code, regs, address))
+		if (emulate_vsyscall_pf(error_code, regs, address))
 			return;
 	}
 #endif
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 09/16] x86/traps: Consolidate user fixups in exc_general_protection()
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (7 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 08/16] x86/vsyscall: Reorganize the #PF emulation code Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 10/16] x86/vsyscall: Add vsyscall emulation for #GP Alexander Shishkin
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi, Dave Hansen

From: Sohil Mehta <sohil.mehta@intel.com>

Move the UMIP exception fixup along with the other user mode fixups,
that is, under the common "if (user_mode(regs))" condition where the
rest of the fixups reside.

No functional change intended.

Suggested-by: Dave Hansen <dave.hansen@intel.com>
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/traps.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 4fa0b17e5043..ae34e03739cb 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -652,11 +652,6 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 
 	cond_local_irq_enable(regs);
 
-	if (static_cpu_has(X86_FEATURE_UMIP)) {
-		if (user_mode(regs) && fixup_umip_exception(regs))
-			goto exit;
-	}
-
 	if (v8086_mode(regs)) {
 		local_irq_enable();
 		handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
@@ -671,6 +666,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 		if (fixup_vdso_exception(regs, X86_TRAP_GP, error_code, 0))
 			goto exit;
 
+		if (cpu_feature_enabled(X86_FEATURE_UMIP) && fixup_umip_exception(regs))
+			goto exit;
+
 		gp_user_force_sig_segv(regs, X86_TRAP_GP, error_code, desc);
 		goto exit;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 10/16] x86/vsyscall: Add vsyscall emulation for #GP
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (8 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 09/16] x86/traps: Consolidate user fixups in exc_general_protection() Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 11/16] x86/vsyscall: Disable LASS if vsyscall mode is set to EMULATE Alexander Shishkin
                   ` (5 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

The legacy vsyscall page is mapped at a fixed address in the kernel
address range 0xffffffffff600000-0xffffffffff601000. Prior to LASS being
introduced, a legacy vsyscall page access from userspace would always
generate a page fault. The kernel emulates the execute (XONLY) accesses
in the page fault handler and returns back to userspace with the
appropriate register values.

Since LASS intercepts these accesses before the paging structures are
traversed it generates a general protection fault instead of a page
fault. The #GP fault doesn't provide much information in terms of the
error code. So, use the faulting RIP which is preserved in the user
registers to emulate the vsyscall access without going through complex
instruction decoding.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/entry/vsyscall/vsyscall_64.c | 11 ++++++++++-
 arch/x86/include/asm/vsyscall.h       |  6 ++++++
 arch/x86/kernel/traps.c               |  4 ++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
index e89d7d83a594..97608883b4b4 100644
--- a/arch/x86/entry/vsyscall/vsyscall_64.c
+++ b/arch/x86/entry/vsyscall/vsyscall_64.c
@@ -23,7 +23,7 @@
  * soon be no new userspace code that will ever use a vsyscall.
  *
  * The code in this file emulates vsyscalls when notified of a page
- * fault to a vsyscall address.
+ * fault or a general protection fault to a vsyscall address.
  */
 
 #include <linux/kernel.h>
@@ -276,6 +276,15 @@ bool emulate_vsyscall_pf(unsigned long error_code, struct pt_regs *regs,
 	return __emulate_vsyscall(regs, address);
 }
 
+bool emulate_vsyscall_gp(struct pt_regs *regs)
+{
+	/* Emulate only if the RIP points to the vsyscall address */
+	if (!is_vsyscall_vaddr(regs->ip))
+		return false;
+
+	return __emulate_vsyscall(regs, regs->ip);
+}
+
 /*
  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
diff --git a/arch/x86/include/asm/vsyscall.h b/arch/x86/include/asm/vsyscall.h
index 214977f4fa11..4eb8d3673223 100644
--- a/arch/x86/include/asm/vsyscall.h
+++ b/arch/x86/include/asm/vsyscall.h
@@ -16,6 +16,7 @@ extern void set_vsyscall_pgtable_user_bits(pgd_t *root);
  */
 extern bool emulate_vsyscall_pf(unsigned long error_code,
 				struct pt_regs *regs, unsigned long address);
+extern bool emulate_vsyscall_gp(struct pt_regs *regs);
 #else
 static inline void map_vsyscall(void) {}
 static inline bool emulate_vsyscall_pf(unsigned long error_code,
@@ -23,6 +24,11 @@ static inline bool emulate_vsyscall_pf(unsigned long error_code,
 {
 	return false;
 }
+
+static inline bool emulate_vsyscall_gp(struct pt_regs *regs)
+{
+	return false;
+}
 #endif
 
 /*
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index ae34e03739cb..c70d75769b1a 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -67,6 +67,7 @@
 #include <asm/vdso.h>
 #include <asm/tdx.h>
 #include <asm/cfi.h>
+#include <asm/vsyscall.h>
 
 #ifdef CONFIG_X86_64
 #include <asm/x86_init.h>
@@ -669,6 +670,9 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 		if (cpu_feature_enabled(X86_FEATURE_UMIP) && fixup_umip_exception(regs))
 			goto exit;
 
+		if (cpu_feature_enabled(X86_FEATURE_LASS) && emulate_vsyscall_gp(regs))
+			goto exit;
+
 		gp_user_force_sig_segv(regs, X86_TRAP_GP, error_code, desc);
 		goto exit;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 11/16] x86/vsyscall: Disable LASS if vsyscall mode is set to EMULATE
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (9 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 10/16] x86/vsyscall: Add vsyscall emulation for #GP Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 12/16] x86/vsyscall: Document the fact that vsyscall=emulate disables LASS Alexander Shishkin
                   ` (4 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Sohil Mehta <sohil.mehta@intel.com>

The EMULATE mode of vsyscall maps the vsyscall page into user address
space which can be read directly by the user application. This mode has
been deprecated recently and can only be enabled from a special command
line parameter vsyscall=emulate. See commit bf00745e7791 ("x86/vsyscall:
Remove CONFIG_LEGACY_VSYSCALL_EMULATE")

Fixing the LASS violations during the EMULATE mode would need complex
instruction decoding since the resulting #GP fault does not include any
useful error information and the vsyscall address is not readily
available in the RIP.

At this point, no one is expected to be using the insecure and
deprecated EMULATE mode. The rare usages that need support probably
don't care much about security anyway. Disable LASS when EMULATE mode is
requested during command line parsing to avoid breaking user software.
LASS will be supported if vsyscall mode is set to XONLY or NONE.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/entry/vsyscall/vsyscall_64.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
index 97608883b4b4..7c845c1db3b4 100644
--- a/arch/x86/entry/vsyscall/vsyscall_64.c
+++ b/arch/x86/entry/vsyscall/vsyscall_64.c
@@ -36,6 +36,7 @@
 #include <asm/vsyscall.h>
 #include <asm/unistd.h>
 #include <asm/fixmap.h>
+#include <asm/tlbflush.h>
 #include <asm/traps.h>
 #include <asm/paravirt.h>
 
@@ -63,6 +64,13 @@ static int __init vsyscall_setup(char *str)
 		else
 			return -EINVAL;
 
+		if (cpu_feature_enabled(X86_FEATURE_LASS) &&
+		    vsyscall_mode == EMULATE) {
+			cr4_clear_bits(X86_CR4_LASS);
+			setup_clear_cpu_cap(X86_FEATURE_LASS);
+			pr_warn_once("x86/cpu: Disabling LASS support due to vsyscall=emulate\n");
+		}
+
 		return 0;
 	}
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 12/16] x86/vsyscall: Document the fact that vsyscall=emulate disables LASS
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (10 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 11/16] x86/vsyscall: Disable LASS if vsyscall mode is set to EMULATE Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 13/16] x86/cpu: Set LASS CR4 bit as pinning sensitive Alexander Shishkin
                   ` (3 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi, Dave Hansen

Since EMULATE mode of vsyscall disables LASS, because fixing the LASS
violations during the EMULATE mode would need complex instruction
decoding, document this fact in kernel-parameters.txt.

Cc: Andy Lutomirski <luto@kernel.org>
Suggested-by: Dave Hansen <dave.hansen@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 Documentation/admin-guide/kernel-parameters.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 27ec49af1bf2..f7f06049353c 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -7247,7 +7247,9 @@
 
 			emulate     Vsyscalls turn into traps and are emulated
 			            reasonably safely.  The vsyscall page is
-				    readable.
+				    readable.  This also disables the LASS
+				    feature to allow userspace to poke around
+				    the vsyscall page.
 
 			xonly       [default] Vsyscalls turn into traps and are
 			            emulated reasonably safely.  The vsyscall
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 13/16] x86/cpu: Set LASS CR4 bit as pinning sensitive
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (11 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 12/16] x86/vsyscall: Document the fact that vsyscall=emulate disables LASS Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 14/16] x86/traps: Communicate a LASS violation in #GP message Alexander Shishkin
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

From: Yian Chen <yian.chen@intel.com>

Security features such as LASS are not expected to be disabled once
initialized. Add LASS to the CR4 pinned mask.

Signed-off-by: Yian Chen <yian.chen@intel.com>
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Reviewed-by: Tony Luck <tony.luck@intel.com>
---
 arch/x86/kernel/cpu/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index c93c59a27dfa..3dc443c349f0 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -407,7 +407,8 @@ static __always_inline void setup_lass(struct cpuinfo_x86 *c)
 
 /* These bits should not change their value after CPU init is finished. */
 static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
-					     X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED;
+					     X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED |
+					     X86_CR4_LASS;
 static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
 static unsigned long cr4_pinned_bits __ro_after_init;
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 14/16] x86/traps: Communicate a LASS violation in #GP message
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (12 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 13/16] x86/cpu: Set LASS CR4 bit as pinning sensitive Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 15/16] efi: Disable LASS around set_virtual_address_map call Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 16/16] x86/cpu: Make LAM depend on LASS Alexander Shishkin
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

Provide a more helpful message on #GP when a kernel side LASS violation
is detected.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/traps.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index c70d75769b1a..42c032106024 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -488,7 +488,8 @@ DEFINE_IDTENTRY(exc_bounds)
 enum kernel_gp_hint {
 	GP_NO_HINT,
 	GP_NON_CANONICAL,
-	GP_CANONICAL
+	GP_CANONICAL,
+	GP_LASS_VIOLATION
 };
 
 /*
@@ -524,6 +525,8 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
 	if (*addr < ~__VIRTUAL_MASK &&
 	    *addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
 		return GP_NON_CANONICAL;
+	else if (*addr < ~__VIRTUAL_MASK && cpu_feature_enabled(X86_FEATURE_LASS))
+		return GP_LASS_VIOLATION;
 #endif
 
 	return GP_CANONICAL;
@@ -647,6 +650,11 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 	char desc[sizeof(GPFSTR) + 50 + 2*sizeof(unsigned long) + 1] = GPFSTR;
 	enum kernel_gp_hint hint = GP_NO_HINT;
 	unsigned long gp_addr;
+	static char *help[] = {
+		[GP_NON_CANONICAL]	= "probably for non-canonical address",
+		[GP_CANONICAL]		= "maybe for address",
+		[GP_LASS_VIOLATION]	= "LASS prevented access to address"
+	};
 
 	if (user_mode(regs) && try_fixup_enqcmd_gp())
 		return;
@@ -686,9 +694,7 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
 		hint = get_kernel_gp_address(regs, &gp_addr);
 
 	if (hint != GP_NO_HINT)
-		snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
-			 (hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
-						    : "maybe for address",
+		snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx", help[hint],
 			 gp_addr);
 
 	/*
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 15/16] efi: Disable LASS around set_virtual_address_map call
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (13 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 14/16] x86/traps: Communicate a LASS violation in #GP message Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  2024-07-10 16:06 ` [PATCH v4 16/16] x86/cpu: Make LAM depend on LASS Alexander Shishkin
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

Of all the EFI runtime services, set_virtual_address_map is the only one
that is called at its lower mapping, which LASS prohibits regardless of
EFLAGS.AC setting. The only way to allow this to happen is to disable
LASS in the CR4 register.

Disable LASS around this low address EFI call.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/platform/efi/efi.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
index f090ec972d7b..6e5c2bb4f4df 100644
--- a/arch/x86/platform/efi/efi.c
+++ b/arch/x86/platform/efi/efi.c
@@ -848,11 +848,24 @@ static void __init __efi_enter_virtual_mode(void)
 
 	efi_sync_low_kernel_mappings();
 
+	/*
+	 * set_virtual_address_map is the only service located at lower
+	 * addresses, so we have to temporarily disable LASS around it.
+	 * Note that clearing EFLAGS.AC is not enough for this, the whole
+	 * LASS needs to be disabled.
+	 */
+	if (cpu_feature_enabled(X86_FEATURE_LASS))
+		cr4_clear_bits(X86_CR4_LASS);
+
 	status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
 					     efi.memmap.desc_size,
 					     efi.memmap.desc_version,
 					     (efi_memory_desc_t *)pa,
 					     efi_systab_phys);
+
+	if (cpu_feature_enabled(X86_FEATURE_LASS))
+		cr4_set_bits(X86_CR4_LASS);
+
 	if (status != EFI_SUCCESS) {
 		pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
 		       status);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 16/16] x86/cpu: Make LAM depend on LASS
  2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
                   ` (14 preceding siblings ...)
  2024-07-10 16:06 ` [PATCH v4 15/16] efi: Disable LASS around set_virtual_address_map call Alexander Shishkin
@ 2024-07-10 16:06 ` Alexander Shishkin
  15 siblings, 0 replies; 30+ messages in thread
From: Alexander Shishkin @ 2024-07-10 16:06 UTC (permalink / raw)
  To: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Alexander Shishkin, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

To prevent exploits for Spectre based on LAM as demonstrated by the
whitepaper [1], make LAM depend on LASS, which avoids this type of
vulnerability.

[1] https://download.vusec.net/papers/slam_sp24.pdf

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 arch/x86/kernel/cpu/cpuid-deps.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index 22612e01ec2e..8986e990beb0 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -85,6 +85,7 @@ static const struct cpuid_dep cpuid_deps[] = {
 	{ X86_FEATURE_FRED,			X86_FEATURE_LKGS      },
 	{ X86_FEATURE_FRED,			X86_FEATURE_WRMSRNS   },
 	{ X86_FEATURE_LASS,			X86_FEATURE_SMAP      },
+	{ X86_FEATURE_LAM,			X86_FEATURE_LASS      },
 	{}
 };
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 16:06 ` [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives Alexander Shishkin
@ 2024-07-10 17:18   ` Borislav Petkov
  2024-07-10 22:33     ` Kirill A. Shutemov
  0 siblings, 1 reply; 30+ messages in thread
From: Borislav Petkov @ 2024-07-10 17:18 UTC (permalink / raw)
  To: Alexander Shishkin
  Cc: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Dave Hansen, x86,
	H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel, Paul E. McKenney,
	Josh Poimboeuf, Xiongwei Song, Xin Li, Mike Rapoport (IBM),
	Brijesh Singh, Michael Roth, Tony Luck, Kirill A. Shutemov,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Wed, Jul 10, 2024 at 07:06:39PM +0300, Alexander Shishkin wrote:
>  static void text_poke_memcpy(void *dst, const void *src, size_t len)
>  {
> -	memcpy(dst, src, len);
> +	stac();
> +	__inline_memcpy(dst, src, len);
> +	clac();

I think you need LASS-specific stac()/clac() or an alternative_2 or so. You
can't cause that perf penalty on !LASS machines.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup
  2024-07-10 16:06 ` [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup Alexander Shishkin
@ 2024-07-10 17:21   ` Borislav Petkov
  0 siblings, 0 replies; 30+ messages in thread
From: Borislav Petkov @ 2024-07-10 17:21 UTC (permalink / raw)
  To: Alexander Shishkin
  Cc: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Dave Hansen, x86,
	H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel, Paul E. McKenney,
	Josh Poimboeuf, Xiongwei Song, Xin Li, Mike Rapoport (IBM),
	Brijesh Singh, Michael Roth, Tony Luck, Kirill A. Shutemov,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Wed, Jul 10, 2024 at 07:06:41PM +0300, Alexander Shishkin wrote:
> From: Sohil Mehta <sohil.mehta@intel.com>
> 
> The code below the comment is self explanatory. Instead of updating the
> comment with the newly added LASS feature, it is better to just remove
> it.
> 
> Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
> Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> ---
>  arch/x86/kernel/cpu/common.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> index dcf61a66e462..33a76256a6f5 100644
> --- a/arch/x86/kernel/cpu/common.c
> +++ b/arch/x86/kernel/cpu/common.c
> @@ -1841,7 +1841,6 @@ static void identify_cpu(struct cpuinfo_x86 *c)
>  	/* Disable the PN if appropriate */
>  	squash_the_stupid_serial_number(c);
>  
> -	/* Set up SMEP/SMAP/UMIP */
>  	setup_smep(c);
>  	setup_smap(c);
>  	setup_umip(c);
> -- 

A separate patch just for that?! You're kidding, right?

Just merge it with the previous one.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 17:18   ` Borislav Petkov
@ 2024-07-10 22:33     ` Kirill A. Shutemov
  2024-07-10 23:05       ` Dave Hansen
                         ` (2 more replies)
  0 siblings, 3 replies; 30+ messages in thread
From: Kirill A. Shutemov @ 2024-07-10 22:33 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Alexander Shishkin, Andy Lutomirski, Thomas Gleixner, Ingo Molnar,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Wed, Jul 10, 2024 at 07:18:36PM +0200, Borislav Petkov wrote:
> On Wed, Jul 10, 2024 at 07:06:39PM +0300, Alexander Shishkin wrote:
> >  static void text_poke_memcpy(void *dst, const void *src, size_t len)
> >  {
> > -	memcpy(dst, src, len);
> > +	stac();
> > +	__inline_memcpy(dst, src, len);
> > +	clac();
> 
> I think you need LASS-specific stac()/clac() or an alternative_2 or so. You
> can't cause that perf penalty on !LASS machines.

Hm. Do we have text_poke() in hot path?

Even if we do, I doubt flipping AC flag would make any performance
difference in context of all locking and TLB flushing we do in this
codepath.

-- 
  Kiryl Shutsemau / Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 22:33     ` Kirill A. Shutemov
@ 2024-07-10 23:05       ` Dave Hansen
  2024-07-11  8:16         ` Peter Zijlstra
  2024-07-11  1:23       ` Borislav Petkov
  2024-07-11  8:14       ` Peter Zijlstra
  2 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2024-07-10 23:05 UTC (permalink / raw)
  To: Kirill A. Shutemov, Borislav Petkov
  Cc: Alexander Shishkin, Andy Lutomirski, Thomas Gleixner, Ingo Molnar,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On 7/10/24 15:33, Kirill A. Shutemov wrote:
> On Wed, Jul 10, 2024 at 07:18:36PM +0200, Borislav Petkov wrote:
>> On Wed, Jul 10, 2024 at 07:06:39PM +0300, Alexander Shishkin wrote:
>>>  static void text_poke_memcpy(void *dst, const void *src, size_t len)
>>>  {
>>> -	memcpy(dst, src, len);
>>> +	stac();
>>> +	__inline_memcpy(dst, src, len);
>>> +	clac();
>> I think you need LASS-specific stac()/clac() or an alternative_2 or so. You
>> can't cause that perf penalty on !LASS machines.
> Hm. Do we have text_poke() in hot path?
> 
> Even if we do, I doubt flipping AC flag would make any performance
> difference in context of all locking and TLB flushing we do in this
> codepath.

Yeah, I'm also wondering how much this would matter for performance.

But, I'm 100% sure that we want to distinguish a LASS-necessitated
stac()/clac() from a SMAP-necessitated one somehow.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 22:33     ` Kirill A. Shutemov
  2024-07-10 23:05       ` Dave Hansen
@ 2024-07-11  1:23       ` Borislav Petkov
  2024-07-11  8:14       ` Peter Zijlstra
  2 siblings, 0 replies; 30+ messages in thread
From: Borislav Petkov @ 2024-07-11  1:23 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Alexander Shishkin, Andy Lutomirski, Thomas Gleixner, Ingo Molnar,
	Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Thu, Jul 11, 2024 at 01:33:23AM +0300, Kirill A. Shutemov wrote:
> Hm. Do we have text_poke() in hot path?
> 
> Even if we do, I doubt flipping AC flag would make any performance
> difference in context of all locking and TLB flushing we do in this
> codepath.

$ dmesg | grep text_poke | wc -l
237

In a silly guest.

And regardless, we don't do unnecessary toggling of rFLAGS.AC.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization
  2024-07-10 16:06 ` [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization Alexander Shishkin
@ 2024-07-11  8:11   ` Peter Zijlstra
  2024-07-11 10:37     ` Kirill A. Shutemov
  0 siblings, 1 reply; 30+ messages in thread
From: Peter Zijlstra @ 2024-07-11  8:11 UTC (permalink / raw)
  To: Alexander Shishkin
  Cc: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy, Jonathan Corbet,
	Sohil Mehta, Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang,
	Sandipan Das, Breno Leitao, Rick Edgecombe, Yian Chen,
	Alexei Starovoitov, Hou Tao, Juergen Gross, Vegard Nossum,
	Kees Cook, Eric Biggers, Jason Gunthorpe,
	Masami Hiramatsu (Google), Andrew Morton, Luis Chamberlain,
	Yuntao Wang, Rasmus Villemoes, Christophe Leroy, Tejun Heo,
	Changbin Du, Huang Shijie, Geert Uytterhoeven, Namhyung Kim,
	Arnaldo Carvalho de Melo, linux-doc, linux-kernel, linux-efi

On Wed, Jul 10, 2024 at 07:06:43PM +0300, Alexander Shishkin wrote:
> In order to map the EFI runtime services, set_virtual_address_map
> needs to be called, which resides in the lower half of the address
> space. This means that LASS needs to be temporarily disabled around
> this call. This can only be done before the CR pinning is set up.
> 
> Move CR pinning setup behind the EFI initialization.
> 
> Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> Suggested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

So the previous patch makes us not boot, and this fixes it up? Perhaps
order things differently?

> ---
>  arch/x86/kernel/cpu/common.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> index 8aa621dc7d30..c93c59a27dfa 100644
> --- a/arch/x86/kernel/cpu/common.c
> +++ b/arch/x86/kernel/cpu/common.c
> @@ -1948,7 +1948,6 @@ static __init void identify_boot_cpu(void)
>  	enable_sep_cpu();
>  #endif
>  	cpu_detect_tlb(&boot_cpu_data);
> -	setup_cr_pinning();
>  
>  	tsx_init();
>  	tdx_init();
> @@ -2367,10 +2366,16 @@ void __init arch_cpu_finalize_init(void)
>  
>  	/*
>  	 * This needs to follow the FPU initializtion, since EFI depends on it.
> +	 * It also needs to precede the CR pinning setup, because we need to be
> +	 * able to temporarily clear the CR4.LASS bit in order to execute the
> +	 * set_virtual_address_map call, which resides in lower addresses and
> +	 * would trip LASS if enabled.
>  	 */
>  	if (efi_enabled(EFI_RUNTIME_SERVICES))
>  		efi_enter_virtual_mode();
>  
> +	setup_cr_pinning();
> +
>  	/*
>  	 * Ensure that access to the per CPU representation has the initial
>  	 * boot CPU configuration.
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset
  2024-07-10 16:06 ` [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset Alexander Shishkin
@ 2024-07-11  8:13   ` Peter Zijlstra
  0 siblings, 0 replies; 30+ messages in thread
From: Peter Zijlstra @ 2024-07-11  8:13 UTC (permalink / raw)
  To: Alexander Shishkin
  Cc: Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Kirill A. Shutemov, Alexey Kardashevskiy, Jonathan Corbet,
	Sohil Mehta, Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang,
	Sandipan Das, Breno Leitao, Rick Edgecombe, Yian Chen,
	Alexei Starovoitov, Hou Tao, Juergen Gross, Vegard Nossum,
	Kees Cook, Eric Biggers, Jason Gunthorpe,
	Masami Hiramatsu (Google), Andrew Morton, Luis Chamberlain,
	Yuntao Wang, Rasmus Villemoes, Christophe Leroy, Tejun Heo,
	Changbin Du, Huang Shijie, Geert Uytterhoeven, Namhyung Kim,
	Arnaldo Carvalho de Melo, linux-doc, linux-kernel, linux-efi

On Wed, Jul 10, 2024 at 07:06:38PM +0300, Alexander Shishkin wrote:
> From: Peter Zijlstra <peterz@infradead.org>
> 
> Provide inline memcpy and memset functions that can be used instead of
> the GCC builtins whenever necessary.
> 
> Code posted by Peter Zijlstra <peterz@infradead.org>.

We haz a tag for that:

Originally-by: Peter Zijlstra <peterz@infradead.org>

> Link: https://lore.kernel.org/lkml/Y759AJ%2F0N9fqwDED@hirez.programming.kicks-ass.net/
> [Missing Signed-off-by from PeterZ]

There, lemme fix that for you:

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

> Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
> Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> ---
>  arch/x86/include/asm/string.h | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/arch/x86/include/asm/string.h b/arch/x86/include/asm/string.h
> index c3c2c1914d65..9cb5aae7fba9 100644
> --- a/arch/x86/include/asm/string.h
> +++ b/arch/x86/include/asm/string.h
> @@ -1,6 +1,32 @@
>  /* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _ASM_X86_STRING_H
> +#define _ASM_X86_STRING_H
> +
>  #ifdef CONFIG_X86_32
>  # include <asm/string_32.h>
>  #else
>  # include <asm/string_64.h>
>  #endif
> +
> +static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
> +{
> +	void *ret = to;
> +
> +	asm volatile("rep movsb"
> +		     : "+D" (to), "+S" (from), "+c" (len)
> +		     : : "memory");
> +	return ret;
> +}
> +
> +static __always_inline void *__inline_memset(void *s, int v, size_t n)
> +{
> +	void *ret = s;
> +
> +	asm volatile("rep stosb"
> +		     : "+D" (s), "+c" (n)
> +		     : "a" ((uint8_t)v)
> +		     : "memory");
> +	return ret;
> +}
> +
> +#endif /* _ASM_X86_STRING_H */
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 22:33     ` Kirill A. Shutemov
  2024-07-10 23:05       ` Dave Hansen
  2024-07-11  1:23       ` Borislav Petkov
@ 2024-07-11  8:14       ` Peter Zijlstra
  2 siblings, 0 replies; 30+ messages in thread
From: Peter Zijlstra @ 2024-07-11  8:14 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Borislav Petkov, Alexander Shishkin, Andy Lutomirski,
	Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
	Ard Biesheuvel, Paul E. McKenney, Josh Poimboeuf, Xiongwei Song,
	Xin Li, Mike Rapoport (IBM), Brijesh Singh, Michael Roth,
	Tony Luck, Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta,
	Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Thu, Jul 11, 2024 at 01:33:23AM +0300, Kirill A. Shutemov wrote:
> On Wed, Jul 10, 2024 at 07:18:36PM +0200, Borislav Petkov wrote:
> > On Wed, Jul 10, 2024 at 07:06:39PM +0300, Alexander Shishkin wrote:
> > >  static void text_poke_memcpy(void *dst, const void *src, size_t len)
> > >  {
> > > -	memcpy(dst, src, len);
> > > +	stac();
> > > +	__inline_memcpy(dst, src, len);
> > > +	clac();
> > 
> > I think you need LASS-specific stac()/clac() or an alternative_2 or so. You
> > can't cause that perf penalty on !LASS machines.
> 
> Hm. Do we have text_poke() in hot path?
> 
> Even if we do, I doubt flipping AC flag would make any performance
> difference in context of all locking and TLB flushing we do in this
> codepath.

The one where performance might matter is text_poke_early(), the full
fat one is comically slow as you point out.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-10 23:05       ` Dave Hansen
@ 2024-07-11  8:16         ` Peter Zijlstra
  2024-07-11 10:32           ` Kirill A. Shutemov
  0 siblings, 1 reply; 30+ messages in thread
From: Peter Zijlstra @ 2024-07-11  8:16 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Kirill A. Shutemov, Borislav Petkov, Alexander Shishkin,
	Andy Lutomirski, Thomas Gleixner, Ingo Molnar, Dave Hansen, x86,
	H. Peter Anvin, Ard Biesheuvel, Paul E. McKenney, Josh Poimboeuf,
	Xiongwei Song, Xin Li, Mike Rapoport (IBM), Brijesh Singh,
	Michael Roth, Tony Luck, Alexey Kardashevskiy, Jonathan Corbet,
	Sohil Mehta, Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang,
	Sandipan Das, Breno Leitao, Rick Edgecombe, Yian Chen,
	Alexei Starovoitov, Hou Tao, Juergen Gross, Vegard Nossum,
	Kees Cook, Eric Biggers, Jason Gunthorpe,
	Masami Hiramatsu (Google), Andrew Morton, Luis Chamberlain,
	Yuntao Wang, Rasmus Villemoes, Christophe Leroy, Tejun Heo,
	Changbin Du, Huang Shijie, Geert Uytterhoeven, Namhyung Kim,
	Arnaldo Carvalho de Melo, linux-doc, linux-kernel, linux-efi

On Wed, Jul 10, 2024 at 04:05:31PM -0700, Dave Hansen wrote:

> But, I'm 100% sure that we want to distinguish a LASS-necessitated
> stac()/clac() from a SMAP-necessitated one somehow.

Yeah, if only to make it easier to understand the code.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-11  8:16         ` Peter Zijlstra
@ 2024-07-11 10:32           ` Kirill A. Shutemov
  2024-07-11 14:03             ` Dave Hansen
  0 siblings, 1 reply; 30+ messages in thread
From: Kirill A. Shutemov @ 2024-07-11 10:32 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Dave Hansen, Borislav Petkov, Alexander Shishkin, Andy Lutomirski,
	Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
	Ard Biesheuvel, Paul E. McKenney, Josh Poimboeuf, Xiongwei Song,
	Xin Li, Mike Rapoport (IBM), Brijesh Singh, Michael Roth,
	Tony Luck, Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta,
	Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Thu, Jul 11, 2024 at 10:16:28AM +0200, Peter Zijlstra wrote:
> On Wed, Jul 10, 2024 at 04:05:31PM -0700, Dave Hansen wrote:
> 
> > But, I'm 100% sure that we want to distinguish a LASS-necessitated
> > stac()/clac() from a SMAP-necessitated one somehow.
> 
> Yeah, if only to make it easier to understand the code.

lass_disable()/lass_enable()?

-- 
  Kiryl Shutsemau / Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization
  2024-07-11  8:11   ` Peter Zijlstra
@ 2024-07-11 10:37     ` Kirill A. Shutemov
  0 siblings, 0 replies; 30+ messages in thread
From: Kirill A. Shutemov @ 2024-07-11 10:37 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Alexander Shishkin, Andy Lutomirski, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Ard Biesheuvel,
	Paul E. McKenney, Josh Poimboeuf, Xiongwei Song, Xin Li,
	Mike Rapoport (IBM), Brijesh Singh, Michael Roth, Tony Luck,
	Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta, Ingo Molnar,
	Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On Thu, Jul 11, 2024 at 10:11:53AM +0200, Peter Zijlstra wrote:
> On Wed, Jul 10, 2024 at 07:06:43PM +0300, Alexander Shishkin wrote:
> > In order to map the EFI runtime services, set_virtual_address_map
> > needs to be called, which resides in the lower half of the address
> > space. This means that LASS needs to be temporarily disabled around
> > this call. This can only be done before the CR pinning is set up.
> > 
> > Move CR pinning setup behind the EFI initialization.
> > 
> > Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> > Suggested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> 
> So the previous patch makes us not boot, and this fixes it up? Perhaps
> order things differently?

Maybe just move LASS enabling (patch 04/16) to the very end of the
patchset?

-- 
  Kiryl Shutsemau / Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives
  2024-07-11 10:32           ` Kirill A. Shutemov
@ 2024-07-11 14:03             ` Dave Hansen
  0 siblings, 0 replies; 30+ messages in thread
From: Dave Hansen @ 2024-07-11 14:03 UTC (permalink / raw)
  To: Kirill A. Shutemov, Peter Zijlstra
  Cc: Borislav Petkov, Alexander Shishkin, Andy Lutomirski,
	Thomas Gleixner, Ingo Molnar, Dave Hansen, x86, H. Peter Anvin,
	Ard Biesheuvel, Paul E. McKenney, Josh Poimboeuf, Xiongwei Song,
	Xin Li, Mike Rapoport (IBM), Brijesh Singh, Michael Roth,
	Tony Luck, Alexey Kardashevskiy, Jonathan Corbet, Sohil Mehta,
	Ingo Molnar, Pawan Gupta, Daniel Sneddon, Kai Huang, Sandipan Das,
	Breno Leitao, Rick Edgecombe, Yian Chen, Alexei Starovoitov,
	Hou Tao, Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On 7/11/24 03:32, Kirill A. Shutemov wrote:
> On Thu, Jul 11, 2024 at 10:16:28AM +0200, Peter Zijlstra wrote:
>> On Wed, Jul 10, 2024 at 04:05:31PM -0700, Dave Hansen wrote:
>>
>>> But, I'm 100% sure that we want to distinguish a LASS-necessitated
>>> stac()/clac() from a SMAP-necessitated one somehow.
>> Yeah, if only to make it easier to understand the code.
> lass_disable()/lass_enable()?

I was thinking just lass_clac()/lass_stac() because it's not _really_
disabling LASS.

But I don't feel that strongly about it.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits
  2024-07-10 16:06 ` [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits Alexander Shishkin
@ 2025-05-13  3:08   ` Xin Li
  0 siblings, 0 replies; 30+ messages in thread
From: Xin Li @ 2025-05-13  3:08 UTC (permalink / raw)
  To: Alexander Shishkin, Andy Lutomirski, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra,
	Ard Biesheuvel, Paul E. McKenney, Josh Poimboeuf, Xiongwei Song,
	Xin Li, Mike Rapoport (IBM), Brijesh Singh, Michael Roth,
	Tony Luck, Kirill A. Shutemov, Alexey Kardashevskiy
  Cc: Jonathan Corbet, Sohil Mehta, Ingo Molnar, Pawan Gupta,
	Daniel Sneddon, Kai Huang, Sandipan Das, Breno Leitao,
	Rick Edgecombe, Yian Chen, Alexei Starovoitov, Hou Tao,
	Juergen Gross, Vegard Nossum, Kees Cook, Eric Biggers,
	Jason Gunthorpe, Masami Hiramatsu (Google), Andrew Morton,
	Luis Chamberlain, Yuntao Wang, Rasmus Villemoes, Christophe Leroy,
	Tejun Heo, Changbin Du, Huang Shijie, Geert Uytterhoeven,
	Namhyung Kim, Arnaldo Carvalho de Melo, linux-doc, linux-kernel,
	linux-efi

On 7/10/2024 9:06 AM, Alexander Shishkin wrote:
> diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h
> index c492bdc97b05..76c7d362af94 100644
> --- a/arch/x86/include/asm/disabled-features.h
> +++ b/arch/x86/include/asm/disabled-features.h
> @@ -22,12 +22,14 @@
>   # define DISABLE_CYRIX_ARR	(1<<(X86_FEATURE_CYRIX_ARR & 31))
>   # define DISABLE_CENTAUR_MCR	(1<<(X86_FEATURE_CENTAUR_MCR & 31))
>   # define DISABLE_PCID		0
> +# define DISABLE_LASS		0
>   #else
>   # define DISABLE_VME		0
>   # define DISABLE_K6_MTRR	0
>   # define DISABLE_CYRIX_ARR	0
>   # define DISABLE_CENTAUR_MCR	0
>   # define DISABLE_PCID		(1<<(X86_FEATURE_PCID & 31))
> +# define DISABLE_LASS		(1<<(X86_FEATURE_LASS & 31))
>   #endif /* CONFIG_X86_64 */
>   

You don't need to add DISABLE_LASS, because you don't make the LASS code
optional at build time, i.e., you don't have CONFIG_X86_LASS.

>   #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> @@ -146,7 +148,7 @@
>   #define DISABLED_MASK11	(DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \
>   			 DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK)
>   #define DISABLED_MASK12	(DISABLE_FRED|DISABLE_LAM)
> -#define DISABLED_MASK13	0
> +#define DISABLED_MASK13	(DISABLE_LASS)

The exact same mistake I made ;)

https://lore.kernel.org/lkml/aaed79d5-d683-d1bc-7ba1-b33c8d6db618@suse.com/

>   #define DISABLED_MASK14	0
>   #define DISABLED_MASK15	0
>   #define DISABLED_MASK16	(DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \

     Xin

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2025-05-13  3:10 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-10 16:06 [PATCH v4 00/16] Enable Linear Address Space Separation support Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 01/16] x86/cpu: Enumerate the LASS feature bits Alexander Shishkin
2025-05-13  3:08   ` Xin Li
2024-07-10 16:06 ` [PATCH v4 02/16] x86/asm: Introduce inline memcpy and memset Alexander Shishkin
2024-07-11  8:13   ` Peter Zijlstra
2024-07-10 16:06 ` [PATCH v4 03/16] x86/alternatives: Disable LASS when patching kernel alternatives Alexander Shishkin
2024-07-10 17:18   ` Borislav Petkov
2024-07-10 22:33     ` Kirill A. Shutemov
2024-07-10 23:05       ` Dave Hansen
2024-07-11  8:16         ` Peter Zijlstra
2024-07-11 10:32           ` Kirill A. Shutemov
2024-07-11 14:03             ` Dave Hansen
2024-07-11  1:23       ` Borislav Petkov
2024-07-11  8:14       ` Peter Zijlstra
2024-07-10 16:06 ` [PATCH v4 04/16] x86/cpu: Enable LASS during CPU initialization Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 05/16] x86/cpu: Remove redundant comment during feature setup Alexander Shishkin
2024-07-10 17:21   ` Borislav Petkov
2024-07-10 16:06 ` [PATCH v4 06/16] init/main.c: Move EFI runtime service initialization to x86/cpu Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 07/16] x86/cpu: Defer CR pinning setup until after EFI initialization Alexander Shishkin
2024-07-11  8:11   ` Peter Zijlstra
2024-07-11 10:37     ` Kirill A. Shutemov
2024-07-10 16:06 ` [PATCH v4 08/16] x86/vsyscall: Reorganize the #PF emulation code Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 09/16] x86/traps: Consolidate user fixups in exc_general_protection() Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 10/16] x86/vsyscall: Add vsyscall emulation for #GP Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 11/16] x86/vsyscall: Disable LASS if vsyscall mode is set to EMULATE Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 12/16] x86/vsyscall: Document the fact that vsyscall=emulate disables LASS Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 13/16] x86/cpu: Set LASS CR4 bit as pinning sensitive Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 14/16] x86/traps: Communicate a LASS violation in #GP message Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 15/16] efi: Disable LASS around set_virtual_address_map call Alexander Shishkin
2024-07-10 16:06 ` [PATCH v4 16/16] x86/cpu: Make LAM depend on LASS Alexander Shishkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).