Skip to contentSkip to conversation

NX 0

PATCH Superseded Paolo Bonzini 5 participants 18 messages Oct 31, 2019 – Nov 2, 2019

Subsystems (7)
Documentation x86 KVM Driver core +3 more subsystems

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

v8 cover NX 0

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 0/5] NX

v7->v8: rebase on top of the annoying affair

Junaid Shahid (2):
kvm: Add helper function for creating VM worker
kvm: x86: mmu: Recovery of shattered NX large pages

Paolo Bonzini (1):
kvm: mmu: ITLB_MULTIHIT mitigation

Pawan Gupta (2):
x86: Add ITLB_MULTIHIT bug infrastructure x86/cpu: Add Tremont to the cpu vulnerability whitelist

 .../ABI/testing/sysfs-devices-system-cpu      |   1 +
 .../admin-guide/kernel-parameters.txt         |  17 ++
 arch/x86/include/asm/cpufeatures.h            |   1 +
 arch/x86/include/asm/kvm_host.h               |   6 +
 arch/x86/include/asm/msr-index.h              |   7 +
 arch/x86/kernel/cpu/bugs.c                    |  24 ++
 arch/x86/kernel/cpu/common.c                  |  67 +++--
 arch/x86/kvm/mmu.c                            | 264 +++++++++++++++++-
 arch/x86/kvm/mmu.h                            |   4 +
 arch/x86/kvm/paging_tmpl.h                    |  29 +-
 arch/x86/kvm/x86.c                            |  20 ++
 drivers/base/cpu.c                            |   8 +
 include/linux/cpu.h                           |   2 +
 include/linux/kvm_host.h                      |   6 +
 virt/kvm/kvm_main.c                           | 114 +++++++-
 15 files changed, 527 insertions(+), 43 deletions(-)
Signature · 1 line
2.21.0
author maintainer
#1 lore raw

PATCH v8 1/5 NX 1

From: Pawan Gupta <[email protected]>
Subject: [PATCH v8 1/5] x86: Add ITLB_MULTIHIT bug infrastructure

Some processors may incur a machine check error possibly resulting in an unrecoverable cpu hang when an instruction fetch encounters a TLB multi-hit in the instruction TLB. This can occur when the page size is changed along with either the physical address or cache type [1].

This issue affects both bare-metal x86 page tables and EPT.

This can be mitigated by either eliminating the use of large pages or by using careful TLB invalidations when changing the page size in the page tables.

Just like Spectre, Meltdown, L1TF and MDS, a new bit has been allocated in MSR_IA32_ARCH_CAPABILITIES (PSCHANGE_MC_NO) and will be set on CPUs which are mitigated against this issue.

[1] For example please refer to erratum SKL002 in "6th Generation Intel Processor Family Specification Update"
https://www.intel.com/content/www/us/en/products/docs/processors/core/desktop-6th-gen-core-family-spec-update.html
https://www.google.com/search?q=site:intel.com+SKL002

There are a lot of other affected processors outside of Skylake and that the erratum(referred above) does not fully disclose the issue and the impact, both on Skylake and across all the affected CPUs.

---
 .../ABI/testing/sysfs-devices-system-cpu      |  1 +
 arch/x86/include/asm/cpufeatures.h            |  1 +
 arch/x86/include/asm/msr-index.h              |  7 ++
 arch/x86/kernel/cpu/bugs.c                    | 13 ++++
 arch/x86/kernel/cpu/common.c                  | 65 ++++++++++---------
 drivers/base/cpu.c                            |  8 +++
 include/linux/cpu.h                           |  2 +
 7 files changed, 67 insertions(+), 30 deletions(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +1 -0
index 0e77569bd5e0..fc20cde63d1e 100644
@@ -487,6 +487,7 @@ What:		/sys/devices/system/cpu/vulnerabilities
 		/sys/devices/system/cpu/vulnerabilities/l1tf
 		/sys/devices/system/cpu/vulnerabilities/mds
 		/sys/devices/system/cpu/vulnerabilities/tsx_async_abort
+		/sys/devices/system/cpu/vulnerabilities/itlb_multihit
 Date:		January 2018
 Contact:	Linux kernel mailing list <[email protected]>
 Description:	Information about CPU vulnerabilities
arch/x86/include/asm/cpufeatures.h +1 -0
index 989e03544f18..c4fbe379cc0b 100644
@@ -400,5 +400,6 @@
 #define X86_BUG_MSBDS_ONLY		X86_BUG(20) /* CPU is only affected by the  MSDBS variant of BUG_MDS */
 #define X86_BUG_SWAPGS			X86_BUG(21) /* CPU is affected by speculation through SWAPGS */
 #define X86_BUG_TAA			X86_BUG(22) /* CPU is affected by TSX Async Abort(TAA) */
+#define X86_BUG_ITLB_MULTIHIT		X86_BUG(23) /* CPU may incur MCE during certain page attribute changes */
 
 #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/msr-index.h +7 -0
index b3a8bb2af0b6..6a3124664289 100644
@@ -93,6 +93,13 @@
 						  * Microarchitectural Data
 						  * Sampling (MDS) vulnerabilities.
 						  */
+#define ARCH_CAP_PSCHANGE_MC_NO		BIT(6)	 /*
+						  * The processor is not susceptible to a
+						  * machine check error due to modifying the
+						  * code page size along with either the
+						  * physical address or cache type
+						  * without TLB invalidation.
+						  */
 #define ARCH_CAP_TSX_CTRL_MSR		BIT(7)	/* MSR for TSX control is available. */
 #define ARCH_CAP_TAA_NO			BIT(8)	/*
 						 * Not susceptible to
arch/x86/kernel/cpu/bugs.c +13 -0
index 43c647e19439..5364beda8c61 100644
@@ -1419,6 +1419,11 @@ static ssize_t l1tf_show_state(char *buf)
 }
 #endif
 
+static ssize_t itlb_multihit_show_state(char *buf)
+{
+	return sprintf(buf, "Processor vulnerable\n");
+}
+
 static ssize_t mds_show_state(char *buf)
 {
 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
@@ -1524,6 +1529,9 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr
 	case X86_BUG_TAA:
 		return tsx_async_abort_show_state(buf);
 
+	case X86_BUG_ITLB_MULTIHIT:
+		return itlb_multihit_show_state(buf);
+
 	default:
 		break;
 	}
@@ -1565,4 +1573,9 @@ ssize_t cpu_show_tsx_async_abort(struct device *dev, struct device_attribute *at
 {
 	return cpu_show_common(dev, attr, buf, X86_BUG_TAA);
 }
+
+ssize_t cpu_show_itlb_multihit(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	return cpu_show_common(dev, attr, buf, X86_BUG_ITLB_MULTIHIT);
+}
 #endif
arch/x86/kernel/cpu/common.c +35 -30

95 lines not on this page · raw message

drivers/base/cpu.c +8 -0
index 0fccd8c0312e..6265871a4af2 100644
@@ -561,6 +561,12 @@ ssize_t __weak cpu_show_tsx_async_abort(struct device *dev,
 	return sprintf(buf, "Not affected\n");
 }
 
+ssize_t __weak cpu_show_itlb_multihit(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "Not affected\n");
+}
+
 static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
 static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
 static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
@@ -568,6 +574,7 @@ static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
 static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
 static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
 static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
+static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
 
 static struct attribute *cpu_root_vulnerabilities_attrs[] = {
 	&dev_attr_meltdown.attr,
@@ -577,6 +584,7 @@ static struct attribute *cpu_root_vulnerabilities_attrs[] = {
 	&dev_attr_l1tf.attr,
 	&dev_attr_mds.attr,
 	&dev_attr_tsx_async_abort.attr,
+	&dev_attr_itlb_multihit.attr,
 	NULL
 };
 
include/linux/cpu.h +2 -0
index f35369f79771..2a093434e975 100644
@@ -62,6 +62,8 @@ extern ssize_t cpu_show_mds(struct device *dev,
 extern ssize_t cpu_show_tsx_async_abort(struct device *dev,
 					struct device_attribute *attr,
 					char *buf);
+extern ssize_t cpu_show_itlb_multihit(struct device *dev,
+				      struct device_attribute *attr, char *buf);
 
 extern __printf(4, 5)
 struct device *cpu_device_create(struct device *parent, void *drvdata,
Signature · 1 line
2.21.0
author maintainer
#2 lore raw

PATCH v8 2/5 NX 2

In reply to Paolo Bonzini

From: Pawan Gupta <[email protected]>
Subject: [PATCH v8 2/5] x86/cpu: Add Tremont to the cpu vulnerability whitelist

This patch adds new cpu family ATOM_TREMONT_D to the cpu vunerability whitelist. ATOM_TREMONT_D is not affected by X86_BUG_ITLB_MULTIHIT. There may be more bugs not affecting ATOM_TREMONT_D which are not known at this point and could be added later.

---
 arch/x86/kernel/cpu/common.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/cpu/common.c +2 -0
index d29b71ca3ca7..fffe21945374 100644
@@ -1074,6 +1074,8 @@ static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
 	 * good enough for our purposes.
 	 */
 
+	VULNWL_INTEL(ATOM_TREMONT_D,		NO_ITLB_MULTIHIT),
+
 	/* AMD Family 0xf - 0x12 */
 	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
 	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
Signature · 1 line
2.21.0
author maintainer
#3 lore raw

PATCH v8 3/5 NX 3

In reply to Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

With some Intel processors, putting the same virtual address in the TLB as both a 4 KiB and 2 MiB page can confuse the instruction fetch unit and cause the processor to issue a machine check. Unfortunately if EPT page tables use huge pages, it possible for a malicious guest to cause this situation.

This patch adds a knob to mark huge pages as non-executable. When the nx_huge_pages parameter is enabled (and we are using EPT), all huge pages are marked as NX. If the guest attempts to execute in one of those pages, the page is broken down into 4K pages, which are then marked executable.

This is not an issue for shadow paging (except nested EPT), because then the host is in control of TLB flushes and the problematic situation cannot happen. With nested EPT, again the nested guest can cause problems so we treat shadow and direct EPT the same.

---
 .../admin-guide/kernel-parameters.txt         |  11 ++
 arch/x86/include/asm/kvm_host.h               |   2 +
 arch/x86/kernel/cpu/bugs.c                    |  13 +-
 arch/x86/kvm/mmu.c                            | 135 +++++++++++++++++-
 arch/x86/kvm/paging_tmpl.h                    |  29 +++-
 arch/x86/kvm/x86.c                            |   9 ++
 6 files changed, 186 insertions(+), 13 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +11 -0
index fa8f03ddff24..7cddfc98ffa7 100644
@@ -2055,6 +2055,17 @@
 			KVM MMU at runtime.
 			Default is 0 (off)
 
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.
+
+			If the sw workaround is enabled for the host, guests
+			need not enable it for nested guests.
+
 	kvm-amd.nested=	[KVM,AMD] Allow nested virtualization in KVM/SVM.
 			Default is 1 (enabled)
 
arch/x86/include/asm/kvm_host.h +2 -0
index 24d6598dea29..a37b03483b66 100644
@@ -315,6 +315,7 @@ struct kvm_mmu_page {
 	bool unsync;
 	u8 mmu_valid_gen;
 	bool mmio_cached;
+	bool lpage_disallowed; /* Can't be replaced by an equiv large page */
 
 	/*
 	 * The following two entries are used to key the shadow page in the
@@ -946,6 +947,7 @@ struct kvm_vm_stat {
 	ulong mmu_unsync;
 	ulong remote_tlb_flush;
 	ulong lpages;
+	ulong nx_lpage_splits;
 	ulong max_mmu_page_hash_collisions;
 };
 
arch/x86/kernel/cpu/bugs.c +12 -1
index 5364beda8c61..850005590167 100644
@@ -1257,6 +1257,9 @@ void x86_spec_ctrl_setup_ap(void)
 		x86_amd_ssb_disable();
 }
 
+bool itlb_multihit_kvm_mitigation;
+EXPORT_SYMBOL_GPL(itlb_multihit_kvm_mitigation);
+
 #undef pr_fmt
 #define pr_fmt(fmt)	"L1TF: " fmt
 
@@ -1412,17 +1415,25 @@ static ssize_t l1tf_show_state(char *buf)
 		       l1tf_vmx_states[l1tf_vmx_mitigation],
 		       sched_smt_active() ? "vulnerable" : "disabled");
 }
+
+static ssize_t itlb_multihit_show_state(char *buf)
+{
+	if (itlb_multihit_kvm_mitigation)
+		return sprintf(buf, "KVM: Mitigation: Split huge pages\n");
+	else
+		return sprintf(buf, "KVM: Vulnerable\n");
+}
 #else
 static ssize_t l1tf_show_state(char *buf)
 {
 	return sprintf(buf, "%s\n", L1TF_DEFAULT_MSG);
 }
-#endif
 
 static ssize_t itlb_multihit_show_state(char *buf)
 {
 	return sprintf(buf, "Processor vulnerable\n");
 }
+#endif
 
 static ssize_t mds_show_state(char *buf)
 {
arch/x86/kvm/mmu.c +129 -6

235 lines not on this page · raw message

arch/x86/kvm/paging_tmpl.h +23 -6
index 7d5cdb3af594..97b21e7fd013 100644
@@ -614,13 +614,14 @@ static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
 static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			 struct guest_walker *gw,
 			 int write_fault, int hlevel,
-			 kvm_pfn_t pfn, bool map_writable, bool prefault)
+			 kvm_pfn_t pfn, bool map_writable, bool prefault,
+			 bool lpage_disallowed)
 {
 	struct kvm_mmu_page *sp = NULL;
 	struct kvm_shadow_walk_iterator it;
 	unsigned direct_access, access = gw->pt_access;
 	int top_level, ret;
-	gfn_t base_gfn;
+	gfn_t gfn, base_gfn;
 
 	direct_access = gw->pte_access;
 
@@ -665,13 +666,25 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			link_shadow_page(vcpu, it.sptep, sp);
 	}
 
-	base_gfn = gw->gfn;
+	/*
+	 * FNAME(page_fault) might have clobbered the bottom bits of
+	 * gw->gfn, restore them from the virtual address.
+	 */
+	gfn = gw->gfn | ((addr & PT_LVL_OFFSET_MASK(gw->level)) >> PAGE_SHIFT);
+	base_gfn = gfn;
 
 	trace_kvm_mmu_spte_requested(addr, gw->level, pfn);
 
 	for (; shadow_walk_okay(&it); shadow_walk_next(&it)) {
 		clear_sp_write_flooding_count(it.sptep);
-		base_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
+
+		/*
+		 * We cannot overwrite existing page tables with an NX
+		 * large page, as the leaf could be executable.
+		 */
+		disallowed_hugepage_adjust(it, gfn, &pfn, &hlevel);
+
+		base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
 		if (it.level == hlevel)
 			break;
 
@@ -683,6 +696,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			sp = kvm_mmu_get_page(vcpu, base_gfn, addr,
 					      it.level - 1, true, direct_access);
 			link_shadow_page(vcpu, it.sptep, sp);
+			if (lpage_disallowed)
+				account_huge_nx_page(vcpu->kvm, sp);
 		}
 	}
 
@@ -759,9 +774,11 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
 	int r;
 	kvm_pfn_t pfn;
 	int level = PT_PAGE_TABLE_LEVEL;
-	bool force_pt_level = false;
 	unsigned long mmu_seq;
 	bool map_writable, is_self_change_mapping;
+	bool lpage_disallowed = (error_code & PFERR_FETCH_MASK) &&
+				is_nx_huge_page_enabled();
+	bool force_pt_level = lpage_disallowed;
 
 	pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
 
@@ -851,7 +868,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
 	if (!force_pt_level)
 		transparent_hugepage_adjust(vcpu, walker.gfn, &pfn, &level);
 	r = FNAME(fetch)(vcpu, addr, &walker, write_fault,
-			 level, pfn, map_writable, prefault);
+			 level, pfn, map_writable, prefault, lpage_disallowed);
 	kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
 
 out_unlock:
arch/x86/kvm/x86.c +9 -0
index 32d70ca2a7fd..b087d178a774 100644
@@ -213,6 +213,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ "mmu_unsync", VM_STAT(mmu_unsync) },
 	{ "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
 	{ "largepages", VM_STAT(lpages, .mode = 0444) },
+	{ "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
 	{ "max_mmu_page_hash_collisions",
 		VM_STAT(max_mmu_page_hash_collisions) },
 	{ NULL }
@@ -1279,6 +1280,14 @@ static u64 kvm_get_arch_capabilities(void)
 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
 		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
 
+	/*
+	 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
+	 * the nested hypervisor runs with NX huge pages.  If it is not,
+	 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
+	 * L1 guests, so it need not worry about its own (L2) guests.
+	 */
+	data |= ARCH_CAP_PSCHANGE_MC_NO;
+
 	/*
 	 * If we're doing cache flushes (either "always" or "cond")
 	 * we will do one whenever the guest does a vmlaunch/vmresume.
Signature · 1 line
2.21.0
reviewer
#4 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

---
 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
 arch/x86/kvm/mmu.c                              | 10 ++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
index c667844c1c42..422da241a4cb 100644
@@ -2653,6 +2653,12 @@
 					       ssbd=force-off [ARM64]
 					       l1tf=off [X86]
 					       mds=off [X86]
+					       kvm.nx_huge_pages=off [X86].
+
+				Exceptions:
+					       This does not have any effect on
+					       kvm.nx_huge_pages when
+					       kvm.nx_huge_pages=force.
 
 			auto (default)
 				Mitigate all CPU vulnerabilities, but leave SMT
arch/x86/kvm/mmu.c +8 -2
index e6a5748a12d5..529589a42afb 100644
@@ -6250,6 +6250,12 @@ static void kvm_set_mmio_spte_mask(void)
 	kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
 }
 
+static bool get_nx_auto_mode(void)
+{
+	/* Return true when CPU has the bug, and mitigations are ON */
+	return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
+}
+
 static void __set_nx_huge_pages(bool val)
 {
 	nx_huge_pages = itlb_multihit_kvm_mitigation = val;
@@ -6266,7 +6272,7 @@ static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
 	else if (sysfs_streq(val, "force"))
 		new_val = 1;
 	else if (sysfs_streq(val, "auto"))
-		new_val = boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT);
+		new_val = get_nx_auto_mode();
 	else if (strtobool(val, &new_val) < 0)
 		return -EINVAL;
 
@@ -6296,7 +6302,7 @@ int kvm_mmu_module_init(void)
 	int ret = -ENOMEM;
 
 	if (nx_huge_pages == -1)
-		__set_nx_huge_pages(boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT));
+		__set_nx_huge_pages(get_nx_auto_mode());
 
 	/*
 	 * MMU roles use union aliasing which is, generally speaking, an
Signature · 1 line
2.20.1
author maintainer
#5 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Quoted 23 lines · speck for Pawan Gupta

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Quoted 8 lines

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

Thanks, I'll post v9 soon. Are you going to post backports as bundles on top of Thomas's?

Paolo

Quoted 58 lines
---
 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
 arch/x86/kvm/mmu.c                              | 10 ++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
index c667844c1c42..422da241a4cb 100644
@@ -2653,6 +2653,12 @@
 					       ssbd=force-off [ARM64]
 					       l1tf=off [X86]
 					       mds=off [X86]
+					       kvm.nx_huge_pages=off [X86].
+
+				Exceptions:
+					       This does not have any effect on
+					       kvm.nx_huge_pages when
+					       kvm.nx_huge_pages=force.
 
 			auto (default)
 				Mitigate all CPU vulnerabilities, but leave SMT
arch/x86/kvm/mmu.c +8 -2
index e6a5748a12d5..529589a42afb 100644
@@ -6250,6 +6250,12 @@ static void kvm_set_mmio_spte_mask(void)
 	kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
 }
 
+static bool get_nx_auto_mode(void)
+{
+	/* Return true when CPU has the bug, and mitigations are ON */
+	return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
+}
+
 static void __set_nx_huge_pages(bool val)
 {
 	nx_huge_pages = itlb_multihit_kvm_mitigation = val;
@@ -6266,7 +6272,7 @@ static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
 	else if (sysfs_streq(val, "force"))
 		new_val = 1;
 	else if (sysfs_streq(val, "auto"))
-		new_val = boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT);
+		new_val = get_nx_auto_mode();
 	else if (strtobool(val, &new_val) < 0)
 		return -EINVAL;
 
@@ -6296,7 +6302,7 @@ int kvm_mmu_module_init(void)
 	int ret = -ENOMEM;
 
 	if (nx_huge_pages == -1)
-		__set_nx_huge_pages(boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT));
+		__set_nx_huge_pages(get_nx_auto_mode());
 
 	/*
 	 * MMU roles use union aliasing which is, generally speaking, an
#6 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Quoted 31 lines · speck for Paolo Bonzini
Quoted 30 lines · speck for Pawan Gupta

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

Quoted 6 lines
Quoted 1 line

Thanks, I'll post v9 soon. Are you going to post backports as bundles on top of Thomas's?

Paolo

We (I) have attempted the backports of this and quicly hit issues with the page zapping changes do to code flux in the upstream. My troubles started with with 5.2 and 4.19. I have reached out to an internal resource who is more knowledgable with KVM mm files to assist with that aspect of the backport. The engineer was at a confrence this week and we hope to make more progress next week once he is back.

--mark

#7 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Quoted 34 lines · speck for mark gross
Quoted 33 lines · speck for Paolo Bonzini
Quoted 31 lines · speck for Pawan Gupta

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

Quoted 7 lines
Quoted 4 lines

Thanks, I'll post v9 soon. Are you going to post backports as bundles on top of Thomas's?

Paolo

We (I) have attempted the backports of this and quicly hit issues with the page zapping changes do to code flux in the upstream. My troubles started with with 5.2 and 4.19.

Is this due to the lack of kvm_mmu_zap_all_fast()? I believe that Paolo and others said in an older thread that kvm_mmu_invalidate_zap_all_pages() could be used instead.

That's what I've been planning to do in Ubuntu but I haven't had a chance to test it yet.

Tyler

Quoted 7 lines

I have reached out to an internal resource who is more knowledgable with KVM mm files to assist with that aspect of the backport. The engineer was at a confrence this week and we hope to make more progress next week once he is back.

--mark

#8 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Quoted 43 lines · speck for Tyler Hicks
Quoted 34 lines · speck for mark gross
Quoted 33 lines · speck for Paolo Bonzini
Quoted 31 lines · speck for Pawan Gupta

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

Quoted 7 lines
Quoted 4 lines

Thanks, I'll post v9 soon. Are you going to post backports as bundles on top of Thomas's?

Paolo

We (I) have attempted the backports of this and quicly hit issues with the page zapping changes do to code flux in the upstream. My troubles started with with 5.2 and 4.19.

Quoted 8 lines

Is this due to the lack of kvm_mmu_zap_all_fast()? I believe that Paolo and others said in an older thread that kvm_mmu_invalidate_zap_all_pages() could be used instead.

That's what I've been planning to do in Ubuntu but I haven't had a chance to test it yet.

Tyler

I missed that comment. I'll retry my backporting using your recomendation above.

--mark

Quoted 8 lines
Quoted 8 lines

I have reached out to an internal resource who is more knowledgable with KVM mm files to assist with that aspect of the backport. The engineer was at a confrence this week and we hope to make more progress next week once he is back.

--mark

author maintainer
#9 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

Quoted 8 lines · speck for mark gross
Quoted 8 lines
Quoted 3 lines

We (I) have attempted the backports of this and quicly hit issues with the page zapping changes do to code flux in the upstream. My troubles started with with 5.2 and 4.19.

Is this due to the lack of kvm_mmu_zap_all_fast()? I believe that Paolo and others said in an older thread that kvm_mmu_invalidate_zap_all_pages() could be used instead.

Quoted 5 lines
Quoted 2 lines

That's what I've been planning to do in Ubuntu but I haven't had a chance to test it yet.

I missed that comment. I'll retry my backporting using your recomendation above.

Correct, backports were discussed a couple weeks ago and nothing has changed since then.

Paolo

#10 lore raw

[MODERATED] Re: [PATCH v8 3/5] NX 3

In reply to Pawan Gupta
Quoted 41 lines · speck for Pawan Gupta

Review

Quoted 8 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH v8 3/5] kvm: mmu: ITLB_MULTIHIT mitigation

(quoted diff) +5 -0
+	kvm.nx_huge_pages=
+			[KVM] Controls the sw workaround for bug
+			X86_BUG_ITLB_MULTIHIT.
+			force	: Always deploy workaround.
+			off	: Default. Never deploy workaround.

off is not the default in the code, so the default should be "auto" here.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			auto	: Deploy workaround based on presence of
+				  X86_BUG_ITLB_MULTIHIT.

Also mitigations=off is not disabling this mitigation. Below patch does that when mitigations=off and kvm.nx_huge_pages=auto.

---
From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

---
 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
 arch/x86/kvm/mmu.c                              | 10 ++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index c667844c1c42..422da241a4cb 100644

Documentation/admin-guide/kernel-parameters.txt line 2655

Quoted 3 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
@@ -2653,6 +2653,12 @@
 					       ssbd=force-off [ARM64]
 					       l1tf=off [X86]
 					       mds=off [X86]

This patch is not written against speck/master (which has the TAA patches) and there's a very minor merge conflict here.

Quoted 14 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+					       kvm.nx_huge_pages=off [X86].
+
+				Exceptions:
+					       This does not have any effect on
+					       kvm.nx_huge_pages when
+					       kvm.nx_huge_pages=force.
 
 			auto (default)
 				Mitigate all CPU vulnerabilities, but leave SMT
arch/x86/kvm/mmu.c +0 -0
index e6a5748a12d5..529589a42afb 100644

arch/x86/kvm/mmu.c static void kvm_set_mmio_spte_mask(void) · line 6256

Quoted 7 lines
arch/x86/kvm/mmu.c +4 -0
@@ -6250,6 +6250,12 @@ static void kvm_set_mmio_spte_mask(void)
 	kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
 }
 
+static bool get_nx_auto_mode(void)
+{
+	/* Return true when CPU has the bug, and mitigations are ON */
+	return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();

The call to cpu_mitigations_off() causes a build failure when kvm is built as a module (CONFIG_KVM=m):

ERROR: "cpu_mitigations" [arch/x86/kvm/kvm.ko] undefined! make[2]: *** [__modpost] Error 1
make[1]: *** [modules] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [sub-make] Error 2

The problem is due to cpu_mitigations_off() and cpu_mitigations_auto_nosmt() being inlined in include/linux/cpu.h. Those functions look to only be used in initialization/setup code so I think you could fix this easily enough by unlining and exporting them.

Tyler

Quoted 25 lines
arch/x86/kvm/mmu.c +4 -2
+}
+
 static void __set_nx_huge_pages(bool val)
 {
 	nx_huge_pages = itlb_multihit_kvm_mitigation = val;
@@ -6266,7 +6272,7 @@ static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
 	else if (sysfs_streq(val, "force"))
 		new_val = 1;
 	else if (sysfs_streq(val, "auto"))
-		new_val = boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT);
+		new_val = get_nx_auto_mode();
 	else if (strtobool(val, &new_val) < 0)
 		return -EINVAL;
 
@@ -6296,7 +6302,7 @@ int kvm_mmu_module_init(void)
 	int ret = -ENOMEM;
 
 	if (nx_huge_pages == -1)
-		__set_nx_huge_pages(boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT));
+		__set_nx_huge_pages(get_nx_auto_mode());
 
 	/*
 	 * MMU roles use union aliasing which is, generally speaking, an
Signature · 1 line
2.20.1
#11 lore raw

PATCH 1/1 NX build fixup

From: Tyler Hicks <[email protected]>
Subject: [PATCH] cpu/speculation: Uninline and export CPU mitigations helpers

A kernel module may need to check the value of the "mitigations=" kernel command line parameter as part of its setup when the module needs to perform software mitigations for a CPU flaw. Uninline and export the helper functions surrounding the cpu_mitigations enum to allow for their usage from a module.

---

Only compile tested, with both CONFIG_KVM=y and CONFIG_KVM=m. Paolo, if this looks good to you and Pawan, please include it in NX v9.

 include/linux/cpu.h | 13 ++-----------
 kernel/cpu.c        | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 11 deletions(-)
include/linux/cpu.h +2 -11
index 2a093434e975..f1965255526a 100644
@@ -230,16 +230,7 @@ enum cpu_mitigations {
 
 extern enum cpu_mitigations cpu_mitigations;
 
-/* mitigations=off */
-static inline bool cpu_mitigations_off(void)
-{
-	return cpu_mitigations == CPU_MITIGATIONS_OFF;
-}
-
-/* mitigations=auto,nosmt */
-static inline bool cpu_mitigations_auto_nosmt(void)
-{
-	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
-}
+extern bool cpu_mitigations_off(void);
+extern bool cpu_mitigations_auto_nosmt(void);
 
 #endif /* _LINUX_CPU_H_ */
kernel/cpu.c +14 -0
index fc28e17940e0..bb9f3e636363 100644
@@ -2390,3 +2390,17 @@ static int __init mitigations_parse_cmdline(char *arg)
 	return 0;
 }
 early_param("mitigations", mitigations_parse_cmdline);
+
+/* mitigations=off */
+bool cpu_mitigations_off(void)
+{
+	return cpu_mitigations == CPU_MITIGATIONS_OFF;
+}
+EXPORT_SYMBOL_GPL(cpu_mitigations_off);
+
+/* mitigations=auto,nosmt */
+bool cpu_mitigations_auto_nosmt(void)
+{
+	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
+}
+EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
Signature · 1 line
2.17.1
#12 lore raw

[MODERATED] Re: [PATCH] NX build fixup

Quoted 30 lines · speck for Tyler Hicks

From: Tyler Hicks <[email protected]>
Subject: [PATCH] cpu/speculation: Uninline and export CPU mitigations helpers

A kernel module may need to check the value of the "mitigations=" kernel command line parameter as part of its setup when the module needs to perform software mitigations for a CPU flaw. Uninline and export the helper functions surrounding the cpu_mitigations enum to allow for their usage from a module.

---

Only compile tested, with both CONFIG_KVM=y and CONFIG_KVM=m. Paolo, if this looks good to you and Pawan, please include it in NX v9.

 include/linux/cpu.h | 13 ++-----------
 kernel/cpu.c        | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 11 deletions(-)
include/linux/cpu.h +0 -3
index 2a093434e975..f1965255526a 100644
@@ -230,16 +230,7 @@ enum cpu_mitigations {
 
 extern enum cpu_mitigations cpu_mitigations;
 
-/* mitigations=off */
-static inline bool cpu_mitigations_off(void)
-{

include/linux/cpu.h enum cpu_mitigations { · line 234

Quoted 10 lines
include/linux/cpu.h +2 -8
-	return cpu_mitigations == CPU_MITIGATIONS_OFF;
-}
-
-/* mitigations=auto,nosmt */
-static inline bool cpu_mitigations_auto_nosmt(void)
-{
-	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
-}
+extern bool cpu_mitigations_off(void);
+extern bool cpu_mitigations_auto_nosmt(void);

You could probably also remove the "extern enum cpu_mitigations cpu_mitigations" and just make it a static variable in kernel/cpu.c.

Same with the enum itself, it could be made private.

Or, as a completely different approach, cpu_mitigations could be exported. Then it's just a one-liner patch. Either way works for me...

Signature · 1 line
Josh
#13 lore raw

PATCH v2 1/1 NX build fixup

From: Tyler Hicks <[email protected]>
Subject: [PATCH] cpu/speculation: Uninline and export CPU mitigations helpers

A kernel module may need to check the value of the "mitigations=" kernel command line parameter as part of its setup when the module needs to perform software mitigations for a CPU flaw. Uninline and export the helper functions surrounding the cpu_mitigations enum to allow for their usage from a module. Lastly, privatize the enum and cpu_mitigations variable since the value of cpu_mitigations can be checked with the exported helper functions.

---
 include/linux/cpu.h | 25 ++-----------------------
 kernel/cpu.c        | 27 ++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 24 deletions(-)
include/linux/cpu.h +2 -23
index 2a093434e975..bc6c879bd110 100644
@@ -218,28 +218,7 @@ static inline int cpuhp_smt_enable(void) { return 0; }
 static inline int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) { return 0; }
 #endif
 
-/*
- * These are used for a global "mitigations=" cmdline option for toggling
- * optional CPU mitigations.
- */
-enum cpu_mitigations {
-	CPU_MITIGATIONS_OFF,
-	CPU_MITIGATIONS_AUTO,
-	CPU_MITIGATIONS_AUTO_NOSMT,
-};
-
-extern enum cpu_mitigations cpu_mitigations;
-
-/* mitigations=off */
-static inline bool cpu_mitigations_off(void)
-{
-	return cpu_mitigations == CPU_MITIGATIONS_OFF;
-}
-
-/* mitigations=auto,nosmt */
-static inline bool cpu_mitigations_auto_nosmt(void)
-{
-	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
-}
+extern bool cpu_mitigations_off(void);
+extern bool cpu_mitigations_auto_nosmt(void);
 
 #endif /* _LINUX_CPU_H_ */
kernel/cpu.c +26 -1
index fc28e17940e0..e2cad3ee2ead 100644
@@ -2373,7 +2373,18 @@ void __init boot_cpu_hotplug_init(void)
 	this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
 }
 
-enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
+/*
+ * These are used for a global "mitigations=" cmdline option for toggling
+ * optional CPU mitigations.
+ */
+enum cpu_mitigations {
+	CPU_MITIGATIONS_OFF,
+	CPU_MITIGATIONS_AUTO,
+	CPU_MITIGATIONS_AUTO_NOSMT,
+};
+
+static enum cpu_mitigations cpu_mitigations __ro_after_init =
+	CPU_MITIGATIONS_AUTO;
 
 static int __init mitigations_parse_cmdline(char *arg)
 {
@@ -2390,3 +2401,17 @@ static int __init mitigations_parse_cmdline(char *arg)
 	return 0;
 }
 early_param("mitigations", mitigations_parse_cmdline);
+
+/* mitigations=off */
+bool cpu_mitigations_off(void)
+{
+	return cpu_mitigations == CPU_MITIGATIONS_OFF;
+}
+EXPORT_SYMBOL_GPL(cpu_mitigations_off);
+
+/* mitigations=auto,nosmt */
+bool cpu_mitigations_auto_nosmt(void)
+{
+	return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
+}
+EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#14 lore raw

[MODERATED] Re: [PATCH v2] NX build fixup

Quoted 11 lines · speck for Tyler Hicks

From: Tyler Hicks <[email protected]>
Subject: [PATCH] cpu/speculation: Uninline and export CPU mitigations helpers

A kernel module may need to check the value of the "mitigations=" kernel command line parameter as part of its setup when the module needs to perform software mitigations for a CPU flaw. Uninline and export the helper functions surrounding the cpu_mitigations enum to allow for their usage from a module. Lastly, privatize the enum and cpu_mitigations variable since the value of cpu_mitigations can be checked with the exported helper functions.

Quoted 1 line
Signature · 1 line
Josh
reviewer
#15 lore raw

[MODERATED] Re: [PATCH v2] NX mitigations=off fix

In reply to Tyler Hicks

From: Pawan Gupta <[email protected]>
Date: Wed, 30 Oct 2019 21:28:24 -0700
Subject: [PATCH] kvm: x86: mmu: Apply global mitigations knob to ITLB_MULTIHIT

Problem: The global mitigation knob mitigations=off does not turn off X86_BUG_ITLB_MULTIHIT mitigation.

Fix: Turn off the mitigation when ITLB_MULTIHIT mitigation mode is "auto" and mitigations are turned off globally via cmdline mitigations=off.

---
Rebased on taa-master bundle. I do not have access to Thomas's repo. This needs to go on top of Tyler's fix.

 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
 arch/x86/kvm/mmu.c                              | 10 ++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
index e8e0a140a632..555236b92289 100644
@@ -2654,6 +2654,12 @@
 					       l1tf=off [X86]
 					       mds=off [X86]
 					       tsx_async_abort=off [X86]
+					       kvm.nx_huge_pages=off [X86]
+
+				Exceptions:
+					       This does not have any effect on
+					       kvm.nx_huge_pages when
+					       kvm.nx_huge_pages=force.
 
 			auto (default)
 				Mitigate all CPU vulnerabilities, but leave SMT
arch/x86/kvm/mmu.c +8 -2
index e6a5748a12d5..529589a42afb 100644
@@ -6250,6 +6250,12 @@ static void kvm_set_mmio_spte_mask(void)
 	kvm_mmu_set_mmio_spte_mask(mask, mask, ACC_WRITE_MASK | ACC_USER_MASK);
 }
 
+static bool get_nx_auto_mode(void)
+{
+	/* Return true when CPU has the bug, and mitigations are ON */
+	return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
+}
+
 static void __set_nx_huge_pages(bool val)
 {
 	nx_huge_pages = itlb_multihit_kvm_mitigation = val;
@@ -6266,7 +6272,7 @@ static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
 	else if (sysfs_streq(val, "force"))
 		new_val = 1;
 	else if (sysfs_streq(val, "auto"))
-		new_val = boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT);
+		new_val = get_nx_auto_mode();
 	else if (strtobool(val, &new_val) < 0)
 		return -EINVAL;
 
@@ -6296,7 +6302,7 @@ int kvm_mmu_module_init(void)
 	int ret = -ENOMEM;
 
 	if (nx_huge_pages == -1)
-		__set_nx_huge_pages(boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT));
+		__set_nx_huge_pages(get_nx_auto_mode());
 
 	/*
 	 * MMU roles use union aliasing which is, generally speaking, an
Signature · 1 line
2.20.1
author maintainer
#16 lore raw

PATCH v8 4/5 NX 4

In reply to Paolo Bonzini

From: Junaid Shahid <[email protected]>
Subject: [PATCH v8 4/5] kvm: Add helper function for creating VM worker threads

This adds a function to create a kernel thread associated with a given VM. In particular, it ensures that the worker thread inherits the priority and cgroups of the calling thread.

---
 include/linux/kvm_host.h |  6 +++
 virt/kvm/kvm_main.c      | 84 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
include/linux/kvm_host.h +6 -0
index 719fc3e15ea4..52ed5f66e8f9 100644
@@ -1382,4 +1382,10 @@ static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 }
 #endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */
 
+typedef int (*kvm_vm_thread_fn_t)(struct kvm *kvm, uintptr_t data);
+
+int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
+				uintptr_t data, const char *name,
+				struct task_struct **thread_ptr);
+
 #endif
virt/kvm/kvm_main.c +84 -0

93 lines not on this page · raw message

Signature · 1 line
2.21.0
author maintainer
#17 lore raw

PATCH v8 5/5 NX 5

In reply to Paolo Bonzini

From: Junaid Shahid <[email protected]>
Subject: [PATCH v8 5/5] kvm: x86: mmu: Recovery of shattered NX large pages

The page table pages corresponding to broken down large pages are zapped in FIFO order, so that the large page can potentially be recovered, if it is no longer being used for execution. This removes the performance penalty for walking deeper EPT page tables.

By default, one large page will last about one hour once the guest reaches a steady state.

---
 .../admin-guide/kernel-parameters.txt         |   6 +
 arch/x86/include/asm/kvm_host.h               |   4 +
 arch/x86/kvm/mmu.c                            | 129 ++++++++++++++++++
 arch/x86/kvm/mmu.h                            |   4 +
 arch/x86/kvm/x86.c                            |  11 ++
 virt/kvm/kvm_main.c                           |  30 +++-
 6 files changed, 183 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
index 7cddfc98ffa7..e8e0a140a632 100644
@@ -2066,6 +2066,12 @@
 			If the sw workaround is enabled for the host, guests
 			need not enable it for nested guests.
 
+	kvm.nx_huge_pages_recovery_ratio=
+			[KVM] Controls how many 4KiB pages are periodically zapped
+			back to huge pages.  0 disables the recovery, otherwise if
+			the value is N KVM will zap 1/Nth of the 4KiB pages every
+			minute.  The default is 60.
+
 	kvm-amd.nested=	[KVM,AMD] Allow nested virtualization in KVM/SVM.
 			Default is 1 (enabled)
 
arch/x86/include/asm/kvm_host.h +4 -0
index a37b03483b66..4fc61483919a 100644
@@ -312,6 +312,8 @@ struct kvm_rmap_head {
 struct kvm_mmu_page {
 	struct list_head link;
 	struct hlist_node hash_link;
+	struct list_head lpage_disallowed_link;
+
 	bool unsync;
 	u8 mmu_valid_gen;
 	bool mmio_cached;
@@ -860,6 +862,7 @@ struct kvm_arch {
 	 */
 	struct list_head active_mmu_pages;
 	struct list_head zapped_obsolete_pages;
+	struct list_head lpage_disallowed_mmu_pages;
 	struct kvm_page_track_notifier_node mmu_sp_tracker;
 	struct kvm_page_track_notifier_head track_notifier_head;
 
@@ -934,6 +937,7 @@ struct kvm_arch {
 	bool exception_payload_enabled;
 
 	struct kvm_pmu_event_filter *pmu_event_filter;
+	struct task_struct *nx_lpage_recovery_thread;
 };
 
 struct kvm_vm_stat {
arch/x86/kvm/mmu.c +129 -0

172 lines not on this page · raw message

arch/x86/kvm/mmu.h +4 -0
index 11f8ec89433b..d55674f44a18 100644
@@ -210,4 +210,8 @@ void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn);
 bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
 				    struct kvm_memory_slot *slot, u64 gfn);
 int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu);
+
+int kvm_mmu_post_init_vm(struct kvm *kvm);
+void kvm_mmu_pre_destroy_vm(struct kvm *kvm);
+
 #endif
arch/x86/kvm/x86.c +11 -0
index b087d178a774..a30e9962a6ef 100644
@@ -9456,6 +9456,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
 	INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
 	INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
+	INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
 	INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
 	atomic_set(&kvm->arch.noncoherent_dma_count, 0);
 
@@ -9484,6 +9485,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	return kvm_x86_ops->vm_init(kvm);
 }
 
+int kvm_arch_post_init_vm(struct kvm *kvm)
+{
+	return kvm_mmu_post_init_vm(kvm);
+}
+
 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
 {
 	vcpu_load(vcpu);
@@ -9585,6 +9591,11 @@ int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
 }
 EXPORT_SYMBOL_GPL(x86_set_memory_region);
 
+void kvm_arch_pre_destroy_vm(struct kvm *kvm)
+{
+	kvm_mmu_pre_destroy_vm(kvm);
+}
+
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
 	if (current->mm == kvm->mm) {
virt/kvm/kvm_main.c +29 -1
index 513b49be83e0..f6d4385aad65 100644
@@ -626,6 +626,23 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 	return 0;
 }
 
+/*
+ * Called after the VM is otherwise initialized, but just before adding it to
+ * the vm_list.
+ */
+int __weak kvm_arch_post_init_vm(struct kvm *kvm)
+{
+	return 0;
+}
+
+/*
+ * Called just after removing the VM from the vm_list, but before doing any
+ * other destruction.
+ */
+void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm)
+{
+}
+
 static struct kvm *kvm_create_vm(unsigned long type)
 {
 	int r, i;
@@ -676,10 +693,14 @@ static struct kvm *kvm_create_vm(unsigned long type)
 		rcu_assign_pointer(kvm->buses[i],
 			kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
 		if (!kvm->buses[i])
-			goto out_err;
+			goto out_err_no_mmu_notifier;
 	}
 
 	r = kvm_init_mmu_notifier(kvm);
+	if (r)
+		goto out_err_no_mmu_notifier;
+
+	r = kvm_arch_post_init_vm(kvm);
 	if (r)
 		goto out_err;
 
@@ -692,6 +713,11 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	return kvm;
 
 out_err:
+#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
+	if (kvm->mmu_notifier.ops)
+		mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
+#endif
+out_err_no_mmu_notifier:
 	cleanup_srcu_struct(&kvm->irq_srcu);
 out_err_no_irq_srcu:
 	cleanup_srcu_struct(&kvm->srcu);
@@ -734,6 +760,8 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mutex_lock(&kvm_lock);
 	list_del(&kvm->vm_list);
 	mutex_unlock(&kvm_lock);
+	kvm_arch_pre_destroy_vm(kvm);
+
 	kvm_free_irq_routing(kvm);
 	for (i = 0; i < KVM_NR_BUSES; i++) {
 		struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
Signature · 1 line
2.21.0