FAILED: patch "[PATCH] KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB" failed to apply to 6.12-stable tree
Subsystems (4)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x fa787ac07b3ceb56dd88a62d1866038498e96230
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025071240-phoney-deniable-545a@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From fa787ac07b3ceb56dd88a62d1866038498e96230 Mon Sep 17 00:00:00 2001
From: Manuel Andreas <[email protected]>
Date: Wed, 25 Jun 2025 15:53:19 +0200
Subject: [PATCH] KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB
flush
In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.
However, when non-canonical GVAs are passed, there is currently no filtering in place and they are eventually passed to checked invocations of INVVPID on Intel / INVLPGA on AMD. While AMD's INVLPGA silently ignores non-canonical addresses (effectively a no-op), Intel's INVVPID explicitly signals VM-Fail and ultimately triggers the WARN_ONCE in invvpid_error():
invvpid failed: ext=0x0 vpid=1 gva=0xaaaaaaaaaaaaa000
WARNING: CPU: 6 PID: 326 at arch/x86/kvm/vmx/vmx.c:482
invvpid_error+0x91/0xa0 [kvm_intel]
Modules linked in: kvm_intel kvm 9pnet_virtio irqbypass fuse
CPU: 6 UID: 0 PID: 326 Comm: kvm-vm Not tainted 6.15.0 #14 PREEMPT(voluntary)
RIP: 0010:invvpid_error+0x91/0xa0 [kvm_intel]
Call Trace:
vmx_flush_tlb_gva+0x320/0x490 [kvm_intel]
kvm_hv_vcpu_flush_tlb+0x24f/0x4f0 [kvm]
kvm_arch_vcpu_ioctl_run+0x3013/0x5810 [kvm]Hyper-V documents that invalid GVAs (those that are beyond a partition's GVA space) are to be ignored. While not completely clear whether this ruling also applies to non-canonical GVAs, it is likely fine to make that assumption, and manual testing on Azure confirms "real" Hyper-V interprets the specification in the same way.
Skip non-canonical GVAs when processing the list of address to avoid tripping the INVVPID failure. Alternatively, KVM could filter out "bad" GVAs before inserting into the FIFO, but practically speaking the only downside of pushing validation to the final processing is that doing so is suboptimal for the guest, and no well-behaved guest will request TLB flushes for non-canonical addresses.
arch/x86/kvm/hyperv.c +3 -0
@@ -1979,6 +1979,9 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)if (entries[i] ==KVM_HV_TLB_FLUSHALL_ENTRY ) goto out_flush_all; +if (is_noncanonical_invlpg_address (entries[i], vcpu)) +continue ; +/* * Lower 12 bits of 'address' encode the number of additional * pages to flush.
Sasha Levin
maintainerPATCH 1/5 KVM: x86: drop x86.h include from cpuid.h
From: Maxim Levitsky <[email protected]>
[ Upstream commit e52ad1ddd0a3b07777141ec9406d5dc2c9a0de17 ]
Drop x86.h include from cpuid.h to allow the x86.h to include the cpuid.h instead.
Also fix various places where x86.h was implicitly included via cpuid.h
Stable-dep-of: fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
--- arch/x86/kvm/cpuid.h | 1 - arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mtrr.c | 1 + arch/x86/kvm/vmx/hyperv.c | 1 + arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/sgx.c | 3 +-- 6 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/cpuid.h +0 -1
@@ -2,7 +2,6 @@#ifndef ARCH_X86_KVM_CPUID_H #define ARCH_X86_KVM_CPUID_H -#include "x86.h" #include "reverse_cpuid.h" #include <asm/cpu.h> #include <asm/processor.h>
arch/x86/kvm/mmu.h +1 -0
@@ -4,6 +4,7 @@#include <linux/kvm_host.h> #include "kvm_cache_regs.h" +#include "x86.h" #include "cpuid.h" extern bool __read_mostly enable_mmio_caching;
arch/x86/kvm/mtrr.c +1 -0
@@ -19,6 +19,7 @@#include <asm/mtrr.h> #include "cpuid.h" +#include "x86.h" static u64 *find_mtrr (struct kvm_vcpu *vcpu,unsigned int msr) {
arch/x86/kvm/vmx/hyperv.c +1 -0
@@ -4,6 +4,7 @@#include <linux/errno.h> #include <linux/smp.h> +#include "x86.h" #include "../cpuid.h" #include "hyperv.h" #include "nested.h"
arch/x86/kvm/vmx/nested.c +1 -1
@@ -7,6 +7,7 @@#include <asm/debugreg.h> #include <asm/mmu_context.h> +#include "x86.h" #include "cpuid.h" #include "hyperv.h" #include "mmu.h"
@@ -16,7 +17,6 @@#include "sgx.h" #include "trace.h" #include "vmx.h" -#include "x86.h" #include "smm.h" static bool __read_mostly enable_shadow_vmcs =1 ;
arch/x86/kvm/vmx/sgx.c +1 -2
@@ -4,12 +4,11 @@#include <asm/sgx.h> -#include "cpuid.h" +#include "x86.h" #include "kvm_cache_regs.h" #include "nested.h" #include "sgx.h" #include "vmx.h" -#include "x86.h" bool __read_mostly enable_sgx =1 ;module_param_named (sgx, enable_sgx, bool,0444 );
Signature · 1 line
2.39.5
Sasha Levin
maintainerPATCH 2/5 KVM: x86: Route non-canonical checks in emulator through emulate_ops
From: Maxim Levitsky <[email protected]>
[ Upstream commit 16ccadefa295af434ca296e566f078223ecd79ca ]
Add emulate_ops.is_canonical_addr() to perform (non-)canonical checks in the emulator, which will allow extending is_noncanonical_address() to support different flavors of canonical checks, e.g. for descriptor table bases vs. MSRs, without needing duplicate logic in the emulator.
No functional change is intended.
Stable-dep-of: fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
--- arch/x86/kvm/emulate.c | 2 +- arch/x86/kvm/kvm_emulate.h | 2 ++ arch/x86/kvm/x86.c | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +1 -1
@@ -653,7 +653,7 @@ static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)static inline bool emul_is_noncanonical_address (u64 la,struct x86_emulate_ctxt *ctxt) { -return !__is_canonical_address (la,ctxt_virt_addr_bits (ctxt)); +return !ctxt->ops->is_canonical_addr (ctxt, la); }/*
arch/x86/kvm/kvm_emulate.h +2 -0
@@ -235,6 +235,8 @@ struct x86_emulate_ops {gva_t (*get_untagged_addr)(struct x86_emulate_ctxt *ctxt,gva_t addr,unsigned int flags); + +bool (*is_canonical_addr)(struct x86_emulate_ctxt *ctxt,gva_t addr); };/* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/x86.c +7 -0
@@ -8608,6 +8608,12 @@ static gva_t emulator_get_untagged_addr(struct x86_emulate_ctxt *ctxt, addr, flags); } +static bool emulator_is_canonical_addr (struct x86_emulate_ctxt *ctxt, +gva_t addr) +{ +return !is_noncanonical_address (addr,emul_to_vcpu (ctxt)); +} +static const struct x86_emulate_ops emulate_ops = { .vm_bugged = emulator_vm_bugged, .read_gpr = emulator_read_gpr,
@@ -8654,6 +8660,7 @@ static const struct x86_emulate_ops emulate_ops = { .triple_fault = emulator_triple_fault, .set_xcr = emulator_set_xcr, .get_untagged_addr = emulator_get_untagged_addr, + .is_canonical_addr = emulator_is_canonical_addr, };static void toggle_interruptibility (struct kvm_vcpu *vcpu,u32 mask)
Signature · 1 line
2.39.5
Re: [PATCH 6.12.y 2/5] KVM: x86: Route non-canonical checks in emulator through emulate_ops
Quoted 11 lines · Sasha Levin
From: Maxim Levitsky <[email protected]>
[ Upstream commit
16ccadefa295af434ca296e566f078223ecd79ca]Add
emulate_ops.is_canonical_addr()to perform (non-)canonical checks in the emulator, which will allow extendingis_noncanonical_address()to support different flavors of canonical checks, e.g. for descriptor table bases vs. MSRs, without needing duplicate logic in the emulator.No functional change is intended.
Quoted 7 lines
Signed-off-by Maxim Levitsky <[email protected]>[sean: separate from additional of flags, massage changelog]Signed-off-by Sean Christopherson <[email protected]>Stable-dep-of:
fa787ac07b3c("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")Signed-off-by Sasha Levin <[email protected]>---
Sasha Levin
maintainerPATCH 3/5 KVM: x86: Add X86EMUL_F_MSR and X86EMUL_F_DT_LOAD to aid canonical checks
In reply to Sasha LevinFrom: Maxim Levitsky <[email protected]>
[ Upstream commit c534b37b7584e2abc5d487b4e017f61a61959ca9 ]
Add emulation flags for MSR accesses and Descriptor Tables loads, and pass the new flags as appropriate to emul_is_noncanonical_address(). The flags will be used to perform the correct canonical check, as the type of access affects whether or not CR4.LA57 is consulted when determining the canonical bit.
No functional change is intended.
Stable-dep-of: fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
--- arch/x86/kvm/emulate.c | 15 +++++++++------ arch/x86/kvm/kvm_emulate.h | 5 ++++- arch/x86/kvm/x86.c | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +9 -6
@@ -651,9 +651,10 @@ static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt) }static inline bool emul_is_noncanonical_address (u64 la, -struct x86_emulate_ctxt *ctxt) +struct x86_emulate_ctxt *ctxt, +unsigned int flags) { -return !ctxt->ops->is_canonical_addr (ctxt, la); +return !ctxt->ops->is_canonical_addr (ctxt, la, flags); }/*
@@ -1733,7 +1734,8 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,if (ret !=X86EMUL_CONTINUE )return ret;if (emul_is_noncanonical_address (get_desc_base (&seg_desc) | - ((u64 )base3 <<32 ), ctxt)) + ((u64 )base3 <<32 ), ctxt, +X86EMUL_F_DT_LOAD ))return emulate_gp (ctxt, err_code); }
@@ -2516,8 +2518,8 @@ static int em_sysexit(struct x86_emulate_ctxt *ctxt) ss_sel = cs_sel +8 ; cs.d =0 ; cs.l =1 ; -if (emul_is_noncanonical_address (rcx, ctxt) || -emul_is_noncanonical_address (rdx, ctxt)) +if (emul_is_noncanonical_address (rcx, ctxt,0 ) || +emul_is_noncanonical_address (rdx, ctxt,0 ))return emulate_gp (ctxt,0 );break ; }
@@ -3494,7 +3496,8 @@ static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)if (rc !=X86EMUL_CONTINUE )return rc;if (ctxt->mode ==X86EMUL_MODE_PROT64 && -emul_is_noncanonical_address (desc_ptr.address, ctxt)) +emul_is_noncanonical_address (desc_ptr.address, ctxt, +X86EMUL_F_DT_LOAD ))return emulate_gp (ctxt,0 );if (lgdt) ctxt->ops->set_gdt (ctxt, &desc_ptr);
arch/x86/kvm/kvm_emulate.h +4 -1
@@ -94,6 +94,8 @@ struct x86_instruction_info {#define X86EMUL_F_FETCH BIT(1)#define X86EMUL_F_IMPLICIT BIT(2)#define X86EMUL_F_INVLPG BIT(3) +#define X86EMUL_F_MSR BIT(4) +#define X86EMUL_F_DT_LOAD BIT(5)struct x86_emulate_ops {void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
@@ -236,7 +238,8 @@ struct x86_emulate_ops {gva_t (*get_untagged_addr)(struct x86_emulate_ctxt *ctxt,gva_t addr,unsigned int flags); -bool (*is_canonical_addr)(struct x86_emulate_ctxt *ctxt,gva_t addr); +bool (*is_canonical_addr)(struct x86_emulate_ctxt *ctxt,gva_t addr, +unsigned int flags); };/* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/x86.c +1 -1
@@ -8609,7 +8609,7 @@ static gva_t emulator_get_untagged_addr(struct x86_emulate_ctxt *ctxt, }static bool emulator_is_canonical_addr (struct x86_emulate_ctxt *ctxt, -gva_t addr) +gva_t addr,unsigned int flags) {return !is_noncanonical_address (addr,emul_to_vcpu (ctxt)); }
Signature · 1 line
2.39.5
Re: [PATCH 6.12.y 3/5] KVM: x86: Add X86EMUL_F_MSR and X86EMUL_F_DT_LOAD to aid canonical checks
Quoted 12 lines · Sasha Levin
From: Maxim Levitsky <[email protected]>
[ Upstream commit
c534b37b7584e2abc5d487b4e017f61a61959ca9]Add emulation flags for MSR accesses and Descriptor Tables loads, and pass the new flags as appropriate to
emul_is_noncanonical_address(). The flags will be used to perform the correct canonical check, as the type of access affects whether or not CR4.LA57 is consulted when determining the canonical bit.No functional change is intended.
Quoted 7 lines
Signed-off-by Maxim Levitsky <[email protected]>[sean: split to separate patch, massage changelog]Signed-off-by Sean Christopherson <[email protected]>Stable-dep-of:
fa787ac07b3c("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")Signed-off-by Sasha Levin <[email protected]>---
Sasha Levin
maintainerPATCH 4/5 KVM: x86: model canonical checks more precisely
In reply to Sasha LevinFrom: Maxim Levitsky <[email protected]>
[ Upstream commit 9245fd6b8531497d129a7a6e3eef258042862f85 ]
As a result of a recent investigation, it was determined that x86 CPUs which support 5-level paging, don't always respect CR4.LA57 when doing canonical checks.
In particular:
1. MSRs which contain a linear address, allow full 57-bitcanonical address regardless of CR4.LA57 state. For example: MSR_KERNEL_GS_BASE.
2. All hidden segment bases and GDT/IDT bases also behave like MSRs. This means that full 57-bit canonical address can be loaded to them regardless of CR4.LA57, both using MSRS (e.g GS_BASE) and instructions (e.g LGDT).
3. TLB invalidation instructions also allow the user to use full 57-bit address regardless of the CR4.LA57.
Finally, it must be noted that the CPU doesn't prevent the user from disabling 5-level paging, even when the full 57-bit canonical address is present in one of the registers mentioned above (e.g GDT base).
In fact, this can happen without any userspace help, when the CPU enters SMM mode - some MSRs, for example MSR_KERNEL_GS_BASE are left to contain a non-canonical address in regard to the new mode.
Since most of the affected MSRs and all segment bases can be read and written freely by the guest without any KVM intervention, this patch makes the emulator closely follow hardware behavior, which means that the emulator doesn't take in the account the guest CPUID support for 5-level paging, and only takes in the account the host CPU support.
Stable-dep-of: fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
--- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/vmx/nested.c | 22 ++++++++--------- arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/vmx/sgx.c | 2 +- arch/x86/kvm/vmx/vmx.c | 4 +-- arch/x86/kvm/x86.c | 8 +++--- arch/x86/kvm/x86.h | 48 ++++++++++++++++++++++++++++++++++-- 7 files changed, 66 insertions(+), 22 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -6234,7 +6234,7 @@ void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,/* It's actually a GPA for vcpu->arch.guest_mmu. */ if (mmu != &vcpu->arch.guest_mmu) {/* INVLPG on a non-canonical address is a NOP according to the SDM. */ -if (is_noncanonical_address (addr, vcpu)) +if (is_noncanonical_invlpg_address (addr, vcpu))return ;kvm_x86_call (flush_tlb_gva)(vcpu, addr);
arch/x86/kvm/vmx/nested.c +11 -11
@@ -3020,8 +3020,8 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3)))return -EINVAL ; -if (CC (is_noncanonical_address (vmcs12->host_ia32_sysenter_esp, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_ia32_sysenter_eip, vcpu))) +if (CC (is_noncanonical_msr_address (vmcs12->host_ia32_sysenter_esp, vcpu)) || +CC (is_noncanonical_msr_address (vmcs12->host_ia32_sysenter_eip, vcpu)))return -EINVAL ; if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
@@ -3055,12 +3055,12 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(vmcs12->host_ss_selector == 0 && !ia32e))return -EINVAL ; -if (CC (is_noncanonical_address (vmcs12->host_fs_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_gs_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_gdtr_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_idtr_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_tr_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_rip, vcpu))) +if (CC (is_noncanonical_base_address (vmcs12->host_fs_base, vcpu)) || +CC (is_noncanonical_base_address (vmcs12->host_gs_base, vcpu)) || +CC (is_noncanonical_base_address (vmcs12->host_gdtr_base, vcpu)) || +CC (is_noncanonical_base_address (vmcs12->host_idtr_base, vcpu)) || +CC (is_noncanonical_base_address (vmcs12->host_tr_base, vcpu)) || +CC (is_noncanonical_address (vmcs12->host_rip, vcpu,0 )))return -EINVAL ;/*
@@ -3178,7 +3178,7 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, }if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ) && - (CC (is_noncanonical_address (vmcs12->guest_bndcfgs &PAGE_MASK , vcpu)) || + (CC (is_noncanonical_msr_address (vmcs12->guest_bndcfgs &PAGE_MASK , vcpu)) ||CC ((vmcs12->guest_bndcfgs &MSR_IA32_BNDCFGS_RSVD ))))return -EINVAL ;
@@ -5172,7 +5172,7 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,* non-canonical form. This is the only check on the memory * destination for long mode! */ - exn =is_noncanonical_address (*ret, vcpu); + exn =is_noncanonical_address (*ret, vcpu,0 ); }else {/* * When not in long mode, the virtual/linear address is
@@ -5983,7 +5983,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)* invalidation. */ if (!operand.vpid || -is_noncanonical_address (operand.gla, vcpu)) +is_noncanonical_invlpg_address (operand.gla, vcpu))return nested_vmx_fail (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID );vpid_sync_vcpu_addr (vpid02, operand.gla);
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -365,7 +365,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }break ;case MSR_IA32_DS_AREA : -if (is_noncanonical_address (data, vcpu)) +if (is_noncanonical_msr_address (data, vcpu))return 1 ; pmu->ds_area = data;
arch/x86/kvm/vmx/sgx.c +1 -1
@@ -37,7 +37,7 @@ static int sgx_get_encls_gva(struct kvm_vcpu *vcpu, unsigned long offset, fault = true; }else if (likely (is_64_bit_mode (vcpu))) { *gva =vmx_get_untagged_addr (vcpu, *gva,0 ); - fault =is_noncanonical_address (*gva, vcpu); + fault =is_noncanonical_address (*gva, vcpu,0 ); }else { *gva &=0xffffffff ; fault = (s.unusable) ||
arch/x86/kvm/vmx/vmx.c +2 -2
@@ -2284,7 +2284,7 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))return 1 ; -if (is_noncanonical_address (data &PAGE_MASK , vcpu) || +if (is_noncanonical_msr_address (data &PAGE_MASK , vcpu) || (data &MSR_IA32_BNDCFGS_RSVD ))return 1 ;
@@ -2449,7 +2449,7 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) index = msr_info->index -MSR_IA32_RTIT_ADDR0_A ;if (index >=2 * vmx->pt_desc.num_address_ranges)return 1 ; -if (is_noncanonical_address (data, vcpu)) +if (is_noncanonical_msr_address (data, vcpu))return 1 ;if (index %2 ) vmx->pt_desc.guest.addr_b[index /2 ] = data;
arch/x86/kvm/x86.c +4 -4
@@ -1845,7 +1845,7 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,case MSR_KERNEL_GS_BASE :case MSR_CSTAR :case MSR_LSTAR : -if (is_noncanonical_address (data, vcpu)) +if (is_noncanonical_msr_address (data, vcpu))return 1 ;break ;case MSR_IA32_SYSENTER_EIP :
@@ -1862,7 +1862,7 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,* value, and that something deterministic happens if the guest * invokes 64-bit SYSENTER. */ - data =__canonical_address (data,vcpu_virt_addr_bits (vcpu)); + data =__canonical_address (data,max_host_virt_addr_bits ());break ;case MSR_TSC_AUX :if (!kvm_is_supported_user_return_msr (MSR_TSC_AUX ))
@@ -8611,7 +8611,7 @@ static gva_t emulator_get_untagged_addr(struct x86_emulate_ctxt *ctxt,static bool emulator_is_canonical_addr (struct x86_emulate_ctxt *ctxt,gva_t addr,unsigned int flags) { -return !is_noncanonical_address (addr,emul_to_vcpu (ctxt)); +return !is_noncanonical_address (addr,emul_to_vcpu (ctxt), flags); }static const struct x86_emulate_ops emulate_ops = {
@@ -13763,7 +13763,7 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)* invalidation. */ if ((!pcid_enabled && (operand.pcid !=0 )) || -is_noncanonical_address (operand.gla, vcpu)) { +is_noncanonical_invlpg_address (operand.gla, vcpu)) {kvm_inject_gp (vcpu,0 );return 1 ; }
arch/x86/kvm/x86.h +46 -2
61 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH 6.12.y 4/5] KVM: x86: model canonical checks more precisely
Quoted 35 lines · Sasha Levin
From: Maxim Levitsky <[email protected]>
[ Upstream commit
9245fd6b8531497d129a7a6e3eef258042862f85]As a result of a recent investigation, it was determined that x86 CPUs which support 5-level paging, don't always respect CR4.LA57 when doing canonical checks.
In particular:
1. MSRs which contain a linear address, allow full 57-bitcanonical address regardless of CR4.LA57 state. For example:
MSR_KERNEL_GS_BASE.2. All hidden segment bases and GDT/IDT bases also behave like MSRs. This means that full 57-bit canonical address can be loaded to them regardless of CR4.LA57, both using MSRS (e.g
GS_BASE) and instructions (e.g LGDT).3. TLB invalidation instructions also allow the user to use full 57-bit address regardless of the CR4.LA57.
Finally, it must be noted that the CPU doesn't prevent the user from disabling 5-level paging, even when the full 57-bit canonical address is present in one of the registers mentioned above (e.g GDT base).
In fact, this can happen without any userspace help, when the CPU enters SMM mode - some MSRs, for example
MSR_KERNEL_GS_BASEare left to contain a non-canonical address in regard to the new mode.Since most of the affected MSRs and all segment bases can be read and written freely by the guest without any KVM intervention, this patch makes the emulator closely follow hardware behavior, which means that the emulator doesn't take in the account the guest CPUID support for 5-level paging, and only takes in the account the host CPU support.
Quoted 6 lines
Signed-off-by Maxim Levitsky <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Stable-dep-of:
fa787ac07b3c("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")Signed-off-by Sasha Levin <[email protected]>---
Sasha Levin
maintainerPATCH 5/5 KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush
In reply to Sasha LevinFrom: Manuel Andreas <[email protected]>
[ Upstream commit fa787ac07b3ceb56dd88a62d1866038498e96230 ]
In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.
However, when non-canonical GVAs are passed, there is currently no filtering in place and they are eventually passed to checked invocations of INVVPID on Intel / INVLPGA on AMD. While AMD's INVLPGA silently ignores non-canonical addresses (effectively a no-op), Intel's INVVPID explicitly signals VM-Fail and ultimately triggers the WARN_ONCE in invvpid_error():
invvpid failed: ext=0x0 vpid=1 gva=0xaaaaaaaaaaaaa000
WARNING: CPU: 6 PID: 326 at arch/x86/kvm/vmx/vmx.c:482
invvpid_error+0x91/0xa0 [kvm_intel]
Modules linked in: kvm_intel kvm 9pnet_virtio irqbypass fuse
CPU: 6 UID: 0 PID: 326 Comm: kvm-vm Not tainted 6.15.0 #14 PREEMPT(voluntary)
RIP: 0010:invvpid_error+0x91/0xa0 [kvm_intel]
Call Trace:
vmx_flush_tlb_gva+0x320/0x490 [kvm_intel]
kvm_hv_vcpu_flush_tlb+0x24f/0x4f0 [kvm]
kvm_arch_vcpu_ioctl_run+0x3013/0x5810 [kvm]Hyper-V documents that invalid GVAs (those that are beyond a partition's GVA space) are to be ignored. While not completely clear whether this ruling also applies to non-canonical GVAs, it is likely fine to make that assumption, and manual testing on Azure confirms "real" Hyper-V interprets the specification in the same way.
Skip non-canonical GVAs when processing the list of address to avoid tripping the INVVPID failure. Alternatively, KVM could filter out "bad" GVAs before inserting into the FIFO, but practically speaking the only downside of pushing validation to the final processing is that doing so is suboptimal for the guest, and no well-behaved guest will request TLB flushes for non-canonical addresses.
--- arch/x86/kvm/hyperv.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/hyperv.c +3 -0
@@ -1980,6 +1980,9 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)if (entries[i] ==KVM_HV_TLB_FLUSHALL_ENTRY ) goto out_flush_all; +if (is_noncanonical_invlpg_address (entries[i], vcpu)) +continue ; +/* * Lower 12 bits of 'address' encode the number of additional * pages to flush.
Signature · 1 line
2.39.5
Re: [PATCH 6.12.y 5/5] KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush
Quoted 40 lines · Sasha Levin
From: Manuel Andreas <[email protected]>
[ Upstream commit
fa787ac07b3ceb56dd88a62d1866038498e96230]In KVM guests with Hyper-V hypercalls enabled, the hypercalls
HVCALL_FLUSH_VIRTUAL_ADDRESS_LISTandHVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EXallow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.However, when non-canonical GVAs are passed, there is currently no filtering in place and they are eventually passed to checked invocations of INVVPID on Intel / INVLPGA on AMD. While AMD's INVLPGA silently ignores non-canonical addresses (effectively a no-op), Intel's INVVPID explicitly signals VM-Fail and ultimately triggers the
WARN_ONCEininvvpid_error():invvpid failed: ext=0x0 vpid=1 gva=0xaaaaaaaaaaaaa000
WARNING: CPU: 6 PID: 326 at arch/x86/kvm/vmx/vmx.c:482 invvpid_error+0x91/0xa0 [kvm_intel] Modules linked in: kvm_intel kvm 9pnet_virtio irqbypass fuse CPU: 6 UID: 0 PID: 326 Comm: kvm-vm Not tainted 6.15.0 #14 PREEMPT(voluntary) RIP: 0010:invvpid_error+0x91/0xa0 [kvm_intel] Call Trace: vmx_flush_tlb_gva+0x320/0x490 [kvm_intel] kvm_hv_vcpu_flush_tlb+0x24f/0x4f0 [kvm] kvm_arch_vcpu_ioctl_run+0x3013/0x5810 [kvm]Hyper-V documents that invalid GVAs (those that are beyond a partition's GVA space) are to be ignored. While not completely clear whether this ruling also applies to non-canonical GVAs, it is likely fine to make that assumption, and manual testing on Azure confirms "real" Hyper-V interprets the specification in the same way.
Skip non-canonical GVAs when processing the list of address to avoid tripping the INVVPID failure. Alternatively, KVM could filter out "bad" GVAs before inserting into the FIFO, but practically speaking the only downside of pushing validation to the final processing is that doing so is suboptimal for the guest, and no well-behaved guest will request TLB flushes for non-canonical addresses.
Quoted 8 lines
Fixes 260970862c88 ("KVM: x86: hyper-v: Handle HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST{,EX} calls gently")Signed-off-by Manuel Andreas <[email protected]>Suggested-by Vitaly Kuznetsov <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Sasha Levin <[email protected]>---
Re: [PATCH 6.12.y 1/5] KVM: x86: drop x86.h include from cpuid.h
In reply to Sasha LevinQuoted 9 lines · Sasha Levin
From: Maxim Levitsky <[email protected]>
[ Upstream commit
e52ad1ddd0a3b07777141ec9406d5dc2c9a0de17]Drop
x86.hinclude fromcpuid.hto allow thex86.hto include thecpuid.hinstead.Also fix various places where
x86.hwas implicitly included viacpuid.h
Quoted 7 lines
Signed-off-by Maxim Levitsky <[email protected]>[sean: fixup a missed include in mtrr.c]Signed-off-by Sean Christopherson <[email protected]>Stable-dep-of:
fa787ac07b3c("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")Signed-off-by Sasha Levin <[email protected]>---