Skip to content

Commit 0a096f2

Browse files
committed
Merge tag 'x86-cpu-2021-08-30' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 cache flush updates from Thomas Gleixner: "A reworked version of the opt-in L1D flush mechanism. This is a stop gap for potential future speculation related hardware vulnerabilities and a mechanism for truly security paranoid applications. It allows a task to request that the L1D cache is flushed when the kernel switches to a different mm. This can be requested via prctl(). Changes vs the previous versions: - Get rid of the software flush fallback - Make the handling consistent with other mitigations - Kill the task when it ends up on a SMT enabled core which defeats the purpose of L1D flushing obviously" * tag 'x86-cpu-2021-08-30' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: Documentation: Add L1D flushing Documentation x86, prctl: Hook L1D flushing in via prctl x86/mm: Prepare for opt-in based L1D flush in switch_mm() x86/process: Make room for TIF_SPEC_L1D_FLUSH sched: Add task_work callback for paranoid L1D flush x86/mm: Refactor cond_ibpb() to support other use cases x86/smp: Add a per-cpu view of SMT state
2 parents 7d6e3fa + b7fe54f commit 0a096f2

File tree

15 files changed

+281
-28
lines changed

15 files changed

+281
-28
lines changed

Documentation/admin-guide/hw-vuln/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ are configurable at compile, boot or run time.
1616
multihit.rst
1717
special-register-buffer-data-sampling.rst
1818
core-scheduling.rst
19+
l1d_flush.rst
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
L1D Flushing
2+
============
3+
4+
With an increasing number of vulnerabilities being reported around data
5+
leaks from the Level 1 Data cache (L1D) the kernel provides an opt-in
6+
mechanism to flush the L1D cache on context switch.
7+
8+
This mechanism can be used to address e.g. CVE-2020-0550. For applications
9+
the mechanism keeps them safe from vulnerabilities, related to leaks
10+
(snooping of) from the L1D cache.
11+
12+
13+
Related CVEs
14+
------------
15+
The following CVEs can be addressed by this
16+
mechanism
17+
18+
============= ======================== ==================
19+
CVE-2020-0550 Improper Data Forwarding OS related aspects
20+
============= ======================== ==================
21+
22+
Usage Guidelines
23+
----------------
24+
25+
Please see document: :ref:`Documentation/userspace-api/spec_ctrl.rst
26+
<set_spec_ctrl>` for details.
27+
28+
**NOTE**: The feature is disabled by default, applications need to
29+
specifically opt into the feature to enable it.
30+
31+
Mitigation
32+
----------
33+
34+
When PR_SET_L1D_FLUSH is enabled for a task a flush of the L1D cache is
35+
performed when the task is scheduled out and the incoming task belongs to a
36+
different process and therefore to a different address space.
37+
38+
If the underlying CPU supports L1D flushing in hardware, the hardware
39+
mechanism is used, software fallback for the mitigation, is not supported.
40+
41+
Mitigation control on the kernel command line
42+
---------------------------------------------
43+
44+
The kernel command line allows to control the L1D flush mitigations at boot
45+
time with the option "l1d_flush=". The valid arguments for this option are:
46+
47+
============ =============================================================
48+
on Enables the prctl interface, applications trying to use
49+
the prctl() will fail with an error if l1d_flush is not
50+
enabled
51+
============ =============================================================
52+
53+
By default the mechanism is disabled.
54+
55+
Limitations
56+
-----------
57+
58+
The mechanism does not mitigate L1D data leaks between tasks belonging to
59+
different processes which are concurrently executing on sibling threads of
60+
a physical CPU core when SMT is enabled on the system.
61+
62+
This can be addressed by controlled placement of processes on physical CPU
63+
cores or by disabling SMT. See the relevant chapter in the L1TF mitigation
64+
document: :ref:`Documentation/admin-guide/hw-vuln/l1tf.rst <smt_control>`.
65+
66+
**NOTE** : The opt-in of a task for L1D flushing works only when the task's
67+
affinity is limited to cores running in non-SMT mode. If a task which
68+
requested L1D flushing is scheduled on a SMT-enabled core the kernel sends
69+
a SIGBUS to the task.

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,23 @@
24212421
feature (tagged TLBs) on capable Intel chips.
24222422
Default is 1 (enabled)
24232423

2424+
l1d_flush= [X86,INTEL]
2425+
Control mitigation for L1D based snooping vulnerability.
2426+
2427+
Certain CPUs are vulnerable to an exploit against CPU
2428+
internal buffers which can forward information to a
2429+
disclosure gadget under certain conditions.
2430+
2431+
In vulnerable processors, the speculatively
2432+
forwarded data can be used in a cache side channel
2433+
attack, to access data to which the attacker does
2434+
not have direct access.
2435+
2436+
This parameter controls the mitigation. The
2437+
options are:
2438+
2439+
on - enable the interface for the mitigation
2440+
24242441
l1tf= [X86] Control mitigation of the L1TF vulnerability on
24252442
affected CPUs
24262443

Documentation/userspace-api/spec_ctrl.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,11 @@ Speculation misfeature controls
106106
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_ENABLE, 0, 0);
107107
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_DISABLE, 0, 0);
108108
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_FORCE_DISABLE, 0, 0);
109+
110+
- PR_SPEC_L1D_FLUSH: Flush L1D Cache on context switch out of the task
111+
(works only when tasks run on non SMT cores)
112+
113+
Invocations:
114+
* prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, 0, 0, 0);
115+
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, PR_SPEC_ENABLE, 0, 0);
116+
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_L1D_FLUSH, PR_SPEC_DISABLE, 0, 0);

arch/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,9 @@ config ARCH_SPLIT_ARG64
12821282
config ARCH_HAS_ELFCORE_COMPAT
12831283
bool
12841284

1285+
config ARCH_HAS_PARANOID_L1D_FLUSH
1286+
bool
1287+
12851288
source "kernel/gcov/Kconfig"
12861289

12871290
source "scripts/gcc-plugins/Kconfig"

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ config X86
119119
select ARCH_WANT_HUGE_PMD_SHARE
120120
select ARCH_WANT_LD_ORPHAN_WARN
121121
select ARCH_WANTS_THP_SWAP if X86_64
122+
select ARCH_HAS_PARANOID_L1D_FLUSH
122123
select BUILDTIME_TABLE_SORT
123124
select CLKEVT_I8253
124125
select CLOCKSOURCE_VALIDATE_LAST_CYCLE

arch/x86/include/asm/nospec-branch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
252252
DECLARE_STATIC_KEY_FALSE(mds_user_clear);
253253
DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
254254

255+
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
256+
255257
#include <asm/segment.h>
256258

257259
/**

arch/x86/include/asm/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ struct cpuinfo_x86 {
136136
u16 logical_die_id;
137137
/* Index into per_cpu list: */
138138
u16 cpu_index;
139+
/* Is SMT active on this core? */
140+
bool smt_active;
139141
u32 microcode;
140142
/* Address space bits used by the cache internally */
141143
u8 x86_cache_bits;

arch/x86/include/asm/thread_info.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct thread_info {
8181
#define TIF_SINGLESTEP 4 /* reenable singlestep on user return*/
8282
#define TIF_SSBD 5 /* Speculative store bypass disable */
8383
#define TIF_SPEC_IB 9 /* Indirect branch speculation mitigation */
84-
#define TIF_SPEC_FORCE_UPDATE 10 /* Force speculation MSR update in context switch */
84+
#define TIF_SPEC_L1D_FLUSH 10 /* Flush L1D on mm switches (processes) */
8585
#define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
8686
#define TIF_UPROBE 12 /* breakpointed or singlestepping */
8787
#define TIF_PATCH_PENDING 13 /* pending live patching update */
@@ -93,6 +93,7 @@ struct thread_info {
9393
#define TIF_MEMDIE 20 /* is terminating due to OOM killer */
9494
#define TIF_POLLING_NRFLAG 21 /* idle is polling for TIF_NEED_RESCHED */
9595
#define TIF_IO_BITMAP 22 /* uses I/O bitmap */
96+
#define TIF_SPEC_FORCE_UPDATE 23 /* Force speculation MSR update in context switch */
9697
#define TIF_FORCED_TF 24 /* true if TF in eflags artificially */
9798
#define TIF_BLOCKSTEP 25 /* set when we want DEBUGCTLMSR_BTF */
9899
#define TIF_LAZY_MMU_UPDATES 27 /* task is updating the mmu lazily */
@@ -104,7 +105,7 @@ struct thread_info {
104105
#define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
105106
#define _TIF_SSBD (1 << TIF_SSBD)
106107
#define _TIF_SPEC_IB (1 << TIF_SPEC_IB)
107-
#define _TIF_SPEC_FORCE_UPDATE (1 << TIF_SPEC_FORCE_UPDATE)
108+
#define _TIF_SPEC_L1D_FLUSH (1 << TIF_SPEC_L1D_FLUSH)
108109
#define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
109110
#define _TIF_UPROBE (1 << TIF_UPROBE)
110111
#define _TIF_PATCH_PENDING (1 << TIF_PATCH_PENDING)
@@ -115,6 +116,7 @@ struct thread_info {
115116
#define _TIF_SLD (1 << TIF_SLD)
116117
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
117118
#define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP)
119+
#define _TIF_SPEC_FORCE_UPDATE (1 << TIF_SPEC_FORCE_UPDATE)
118120
#define _TIF_FORCED_TF (1 << TIF_FORCED_TF)
119121
#define _TIF_BLOCKSTEP (1 << TIF_BLOCKSTEP)
120122
#define _TIF_LAZY_MMU_UPDATES (1 << TIF_LAZY_MMU_UPDATES)

arch/x86/include/asm/tlbflush.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct tlb_state {
8383
/* Last user mm for optimizing IBPB */
8484
union {
8585
struct mm_struct *last_user_mm;
86-
unsigned long last_user_mm_ibpb;
86+
unsigned long last_user_mm_spec;
8787
};
8888

8989
u16 loaded_mm_asid;

0 commit comments

Comments
 (0)