KVM: x86: x86.{c,h} spring cleaning
Subsystems (4)
Sean Christopherson
maintainerv4 cover KVM: x86: x86.{c,h} spring cleaning
It's still technically spring!
Perform spring cleaning on x86.{c,h} and asm/kvm_host.h, by adding regs.c (the kvm_cache_regs.h => regs.h is already applied) and msrs.{c,h}, and moving relevant code out of x86.c.
v4:
- Drop the GPR API cleanups (already applied).
- Collect more tags. [Yosry, Kai
- Chunked the x86.c => regs.c code movement into several patches, so that the bulk move is as close to a pure move as is reasonably possible. [Yosry]
- Drop a redundant BUILD_BUG_ON() from store_regs().
- Fix various changelog goofs. [Kai]
- Fix alignment goofs in kvm_get_{,feature_}msr_index_list(). [Kai]
- Drop the defunct x86.h include from mtrr.c. [Kai]
- Fix whitespace goofs. [Kai]
- Rewrite the changelog for the bulk regs.c move to better explain why only a tiny amount of EFER code is moved. [Yosry]
- Move "struct kvm_x86_msr_filter" and "struct kvm_x86_pmu_event_filter" to msrs.c and pmu.c respectively. [Kai]
- Rename LDT helpers to vmx_{load,store}_ldt(), and call out that they will hopefully be replaced by kernel helpers in the near future. [Kai]
- Drop the kvm_cpu_get_apicid() code movement (sent a separate patch to kill it off entirely). [Kai]
- Add more detail to explain why using VALID_MASK defines outside of x86.c is dangerous. [Yosry, Kai]
- Bury kvm_arch_interrupt_allowed() in x86.c as kvm_is_interrupt_allowed(). [Kai]
- Move kvm_find_async_pf_gfn() prototype to x86.h. [Sashiko]
v3:
- Collect more tags. [David, Binbin, Yosry]
- Use kvm_run_sync_regs_{from,to}_user() instead of kvm_run_{g,s}et_regs(). [Kai]
- Fix a variety of typos. [Binbin]
- Everything beyond patch 15...
v2:
- https://lore.kernel.org/all/[email protected]
- Collect tags. [Yosry, Kai]
- Fix some truly egregious goofs. [Binbin]
- Rename kvm_cache_regs.h => regs.h, add regs.c. [Yosry, because he complained, not because he actually suggested this :-D ]
- Drop superfluous casting/masking of e*x() usage. [Kai]
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (30):
KVM: x86: Extract REGS and SREGS runtime sync code to helpers
KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base()
KVM: x86: Rename __{g,s}et_sregs2() =>
kvm_x86_vcpu_ioctl_{g,s}et_sregs2() KVM: x86: Move the bulk of register specific code from x86.c to regs.c
KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
KVM: x86: Swap the include order between x86.h and mmu.h
KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h
KVM: x86: Move eager_page_split to mmu.{c,h}
KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h
KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h}
KVM: x86: Move register helper declarations from kvm_host.h => regs.h
KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h
KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h
KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to
pmu.c
KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h
KVM: x86: Move LLDT assembly wrappers into VMX
KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c
KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h
KVM: x86: Move IRQ-related helper declarations from kvm_host.h =>
irq.h
KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h =>
lapic.h
KVM: x86: Don't treat interrupts as allowed just because a nested run
is pending
KVM: x86: Rework kvm_arch_interrupt_allowed() into
kvm_is_interrupt_allowed()
KVM: x86/mmu: Move kvm_arch_async_page_ready() below
kvm_tdp_page_fault()
KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h =>
mmu.c
KVM: x86: Move a pile of stuff from kvm_host.h => x86.h
arch/x86/include/asm/kvm_host.h | 470 +--- arch/x86/kvm/Makefile | 4 +- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/fpu.h | 26 + arch/x86/kvm/hyperv.h | 3 +- arch/x86/kvm/ioapic.c | 1 + arch/x86/kvm/ioapic.h | 12 + arch/x86/kvm/irq.c | 7 + arch/x86/kvm/irq.h | 6 + arch/x86/kvm/lapic.h | 8 + arch/x86/kvm/mmu.h | 90 +- arch/x86/kvm/mmu/mmu.c | 132 +- arch/x86/kvm/mmu/mmu_internal.h | 66 - arch/x86/kvm/msrs.c | 2745 +++++++++++++++++++++++ arch/x86/kvm/msrs.h | 156 ++ arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/pmu.c | 12 + arch/x86/kvm/regs.c | 875 ++++++++ arch/x86/kvm/regs.h | 60 + arch/x86/kvm/svm/nested.c | 1 + arch/x86/kvm/svm/svm.c | 6 +- arch/x86/kvm/vmx/nested.c | 1 + arch/x86/kvm/vmx/vmx.c | 23 +- arch/x86/kvm/x86.c | 3709 +------------------------------ arch/x86/kvm/x86.h | 354 +-- 25 files changed, 4457 insertions(+), 4313 deletions(-) create mode 100644 arch/x86/kvm/msrs.c create mode 100644 arch/x86/kvm/msrs.h create mode 100644 arch/x86/kvm/regs.c
base-commit: c1f7303302927f9cbf4efedf70f0512cde168c65
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Sean Christopherson
maintainerPATCH v4 1/30 KVM: x86: Extract REGS and SREGS runtime sync code to helpers
Extract the REGS and SREGS portions of {store,sync}_regs() into separate helpers in anticipation of moving the register specific code out of x86.c and into regs.c.
No functional change intended.
--- arch/x86/kvm/x86.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +15 -2
@@ -12687,7 +12687,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)return 0 ; } -static void store_regs (struct kvm_vcpu *vcpu) +static void kvm_run_sync_regs_to_user (struct kvm_vcpu *vcpu) {BUILD_BUG_ON (sizeof (struct kvm_sync_regs ) >SYNC_REGS_SIZE_BYTES );
@@ -12696,13 +12696,18 @@ static void store_regs(struct kvm_vcpu *vcpu)if (vcpu->run->kvm_valid_regs &KVM_SYNC_X86_SREGS )__get_sregs (vcpu, &vcpu->run->s.regs.sregs); +} + +static void store_regs (struct kvm_vcpu *vcpu) +{ +kvm_run_sync_regs_to_user (vcpu);if (vcpu->run->kvm_valid_regs &KVM_SYNC_X86_EVENTS )kvm_vcpu_ioctl_x86_get_vcpu_events ( vcpu, &vcpu->run->s.regs.events); } -static int sync_regs (struct kvm_vcpu *vcpu) +static int kvm_run_sync_regs_from_user (struct kvm_vcpu *vcpu) {if (vcpu->run->kvm_dirty_regs &KVM_SYNC_X86_REGS ) {__set_regs (vcpu, &vcpu->run->s.regs.regs);
@@ -12718,6 +12723,14 @@ static int sync_regs(struct kvm_vcpu *vcpu) vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS ; } +return 0 ; +} + +static int sync_regs (struct kvm_vcpu *vcpu) +{ +if (kvm_run_sync_regs_from_user (vcpu)) +return -EINVAL ; +if (vcpu->run->kvm_dirty_regs &KVM_SYNC_X86_EVENTS ) {struct kvm_vcpu_events events = vcpu->run->s.regs.events;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 01/30] KVM: x86: Extract REGS and SREGS runtime sync code to helpers
Quoted 8 lines · Sean Christopherson
Extract the REGS and SREGS portions of {store,sync}_regs() into separate helpers in anticipation of moving the register specific code out of
x86.cand intoregs.c.No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 01/30] KVM: x86: Extract REGS and SREGS runtime sync code to helpers
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Extract the REGS and SREGS portions of {store,sync}_regs() into separate helpers in anticipation of moving the register specific code out of
x86.cand intoregs.c.No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 52 lines
--- arch/x86/kvm/x86.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +15
-2@@ -12687,7 +12687,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)return 0 ; } -static void store_regs (struct kvm_vcpu *vcpu) +static void kvm_run_sync_regs_to_user (struct kvm_vcpu *vcpu) {BUILD_BUG_ON (sizeof (struct kvm_sync_regs ) >SYNC_REGS_SIZE_BYTES );@@ -12696,13 +12696,18 @@ static void store_regs(struct kvm_vcpu *vcpu)if (vcpu->run->kvm_valid_regs &KVM_SYNC_X86_SREGS )__get_sregs (vcpu, &vcpu->run->s.regs.sregs); +} + +static void store_regs (struct kvm_vcpu *vcpu) +{ +kvm_run_sync_regs_to_user (vcpu);if (vcpu->run->kvm_valid_regs &KVM_SYNC_X86_EVENTS )kvm_vcpu_ioctl_x86_get_vcpu_events ( vcpu, &vcpu->run->s.regs.events); } -static int sync_regs (struct kvm_vcpu *vcpu) +static int kvm_run_sync_regs_from_user (struct kvm_vcpu *vcpu) {if (vcpu->run->kvm_dirty_regs &KVM_SYNC_X86_REGS ) {__set_regs (vcpu, &vcpu->run->s.regs.regs);@@ -12718,6 +12723,14 @@ static int sync_regs(struct kvm_vcpu *vcpu) vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS ; } +return 0 ; +} + +static int sync_regs (struct kvm_vcpu *vcpu) +{ +if (kvm_run_sync_regs_from_user (vcpu)) +return -EINVAL ; +if (vcpu->run->kvm_dirty_regs &KVM_SYNC_X86_EVENTS ) {struct kvm_vcpu_events events = vcpu->run->s.regs.events;
Sean Christopherson
maintainerPATCH v4 2/30 KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base()
In reply to Sean ChristophersonMove get_segment_base() to regs.h, as kvm_get_segment_base(), so that the bulk of the register code can be moved from x86.c to a new regs.c, without simultaneously needing to rename "public" helpers to explicitly scope them to KVM.
No functional change intended.
--- arch/x86/kvm/regs.h | 5 +++++ arch/x86/kvm/x86.c | 9 ++------- 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/regs.h +5 -0
@@ -412,4 +412,9 @@ static inline bool is_guest_mode(struct kvm_vcpu *vcpu) return vcpu->arch.hflags & HF_GUEST_MASK; } +static inline unsigned long kvm_get_segment_base (struct kvm_vcpu *vcpu,int seg) +{ +return kvm_x86_call (get_segment_base)(vcpu, seg); +} +#endif
arch/x86/kvm/x86.c +2 -7
@@ -8492,11 +8492,6 @@ static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,return emulator_pio_out (emul_to_vcpu (ctxt), size, port, val, count); } -static unsigned long get_segment_base (struct kvm_vcpu *vcpu,int seg) -{ -return kvm_x86_call (get_segment_base)(vcpu, seg); -} -static void emulator_invlpg (struct x86_emulate_ctxt *ctxt,ulong address) {kvm_mmu_invlpg (emul_to_vcpu (ctxt), address);
@@ -8641,7 +8636,7 @@ static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)static unsigned long emulator_get_cached_segment_base (struct x86_emulate_ctxt *ctxt,int seg) { -return get_segment_base (emul_to_vcpu (ctxt), seg); +return kvm_get_segment_base (emul_to_vcpu (ctxt), seg); } static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
@@ -13839,7 +13834,7 @@ unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)if (is_64_bit_mode (vcpu))return kvm_rip_read (vcpu); -return (u32)(get_segment_base (vcpu,VCPU_SREG_CS ) + +return (u32)(kvm_get_segment_base (vcpu,VCPU_SREG_CS ) +kvm_rip_read (vcpu)); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_get_linear_rip);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 02/30] KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base()
Quoted 9 lines · Sean Christopherson
Move
get_segment_base()toregs.h, askvm_get_segment_base(), so that the bulk of the register code can be moved fromx86.cto a newregs.c, without simultaneously needing to rename "public" helpers to explicitly scope them to KVM.No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Nit of nit: maybe explicitly say emulator_get_cached_segment_base() in x86.c still calls kvm_get_segment_base() after code movement, so the change here is more obvious.
Re: [PATCH v4 02/30] KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base()
In reply to Sean ChristophersonQuoted 9 lines · Sean Christopherson
Move
get_segment_base()toregs.h, askvm_get_segment_base(), so that the bulk of the register code can be moved fromx86.cto a newregs.c, without simultaneously needing to rename "public" helpers to explicitly scope them to KVM.No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 53 lines
--- arch/x86/kvm/regs.h | 5 +++++ arch/x86/kvm/x86.c | 9 ++------- 2 files changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/regs.h +5
-0@@ -412,4 +412,9 @@ static inline bool is_guest_mode(struct kvm_vcpu *vcpu) return vcpu->arch.hflags & HF_GUEST_MASK; } +static inline unsigned long kvm_get_segment_base (struct kvm_vcpu *vcpu,int seg) +{ +return kvm_x86_call (get_segment_base)(vcpu, seg); +} +#endif arch/x86/kvm/x86.c +2
-7@@ -8492,11 +8492,6 @@ static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,return emulator_pio_out (emul_to_vcpu (ctxt), size, port, val, count); } -static unsigned long get_segment_base (struct kvm_vcpu *vcpu,int seg) -{ -return kvm_x86_call (get_segment_base)(vcpu, seg); -} -static void emulator_invlpg (struct x86_emulate_ctxt *ctxt,ulong address) {kvm_mmu_invlpg (emul_to_vcpu (ctxt), address);@@ -8641,7 +8636,7 @@ static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)static unsigned long emulator_get_cached_segment_base (struct x86_emulate_ctxt *ctxt,int seg) { -return get_segment_base (emul_to_vcpu (ctxt), seg); +return kvm_get_segment_base (emul_to_vcpu (ctxt), seg); } static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,@@ -13839,7 +13834,7 @@ unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)if (is_64_bit_mode (vcpu))return kvm_rip_read (vcpu); -return (u32)(get_segment_base (vcpu,VCPU_SREG_CS ) + +return (u32)(kvm_get_segment_base (vcpu,VCPU_SREG_CS ) +kvm_rip_read (vcpu)); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_get_linear_rip);
Sean Christopherson
maintainerPATCH v4 3/30 KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2()
In reply to Sean ChristophersonRename the KVM_{G,S}ET_SREGS2 helpers in anticipation of moving them out of x86.c (while leaving the ioctl dispatch behind). Having globally visible APIs named __{g,s}et_sregs2() would be "fine", but ugly, given that __{g,s}et_sregs() will NOT be globally visible. As a bonus, this makes it a bit more obvious that the helpers implement newer versions of kvm_arch_vcpu_ioctl_set_sregs().
No functional change intended.
--- arch/x86/kvm/x86.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +10 -6
@@ -133,8 +133,10 @@ static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu); -static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); -static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); +static int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2); +static void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2);static DEFINE_MUTEX (vendor_module_lock);static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu);
@@ -6623,7 +6625,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r = -ENOMEM ;if (!u.sregs2) goto out; -__get_sregs2 (vcpu, u.sregs2); +kvm_x86_vcpu_ioctl_get_sregs2 (vcpu, u.sregs2); r = -EFAULT ;if (copy_to_user (argp, u.sregs2,sizeof (struct kvm_sregs2 ))) goto out;
@@ -6642,7 +6644,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, u.sregs2 =NULL ; goto out; } - r =__set_sregs2 (vcpu, u.sregs2); + r =kvm_x86_vcpu_ioctl_set_sregs2 (vcpu, u.sregs2);break ; }case KVM_HAS_DEVICE_ATTR :
@@ -12210,7 +12212,8 @@ static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) (unsigned long *)sregs->interrupt_bitmap); } -static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2) +static void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2) {int i;
@@ -12480,7 +12483,8 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)return 0 ; } -static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2) +static int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2) {int mmu_reset_needed =0 ;bool valid_pdptrs = sregs2->flags &KVM_SREGS2_FLAGS_PDPTRS_VALID ;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 03/30] KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2()
Quoted 11 lines · Sean Christopherson
Rename the KVM_{G,S}ET_SREGS2 helpers in anticipation of moving them out of
x86.c(while leaving the ioctl dispatch behind). Having globally visible APIs named __{g,s}et_sregs2() would be "fine", but ugly, given that __{g,s}et_sregs() will NOT be globally visible. As a bonus, this makes it a bit more obvious that the helpers implement newer versions ofkvm_arch_vcpu_ioctl_set_sregs().No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 03/30] KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2()
In reply to Sean ChristophersonQuoted 20 lines · Sean Christopherson
Rename the KVM_{G,S}ET_SREGS2 helpers in anticipation of moving them out of
x86.c(while leaving the ioctl dispatch behind). Having globally visible APIs named __{g,s}et_sregs2() would be "fine", but ugly, given that __{g,s}et_sregs() will NOT be globally visible. As a bonus, this makes it a bit more obvious that the helpers implement newer versions ofkvm_arch_vcpu_ioctl_set_sregs().No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); · line 140
Quoted 10 lines
arch/x86/kvm/x86.c +4
-2@@ -133,8 +133,10 @@ static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu); -static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); -static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); +static int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2); +static void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2);
Existing code uses the pattern kvm_vcpu_ioctl_x86_xxx, is it better to align the pattern?
Quoted 40 lines
arch/x86/kvm/x86.c +6
-4static DEFINE_MUTEX (vendor_module_lock);static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu);@@ -6623,7 +6625,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r = -ENOMEM ;if (!u.sregs2) goto out; -__get_sregs2 (vcpu, u.sregs2); +kvm_x86_vcpu_ioctl_get_sregs2 (vcpu, u.sregs2); r = -EFAULT ;if (copy_to_user (argp, u.sregs2,sizeof (struct kvm_sregs2 ))) goto out;@@ -6642,7 +6644,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, u.sregs2 =NULL ; goto out; } - r =__set_sregs2 (vcpu, u.sregs2); + r =kvm_x86_vcpu_ioctl_set_sregs2 (vcpu, u.sregs2);break ; }case KVM_HAS_DEVICE_ATTR :@@ -12210,7 +12212,8 @@ static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) (unsigned long *)sregs->interrupt_bitmap); } -static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2) +static void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2) {int i;@@ -12480,7 +12483,8 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)return 0 ; } -static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2) +static int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2) {int mmu_reset_needed =0 ;bool valid_pdptrs = sregs2->flags &KVM_SREGS2_FLAGS_PDPTRS_VALID ;
Sean Christopherson
maintainerRe: [PATCH v4 03/30] KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2()
Quoted 24 lines · Binbin Wu
Quoted 23 lines · Sean Christopherson
Rename the KVM_{G,S}ET_SREGS2 helpers in anticipation of moving them out of
x86.c(while leaving the ioctl dispatch behind). Having globally visible APIs named __{g,s}et_sregs2() would be "fine", but ugly, given that __{g,s}et_sregs() will NOT be globally visible. As a bonus, this makes it a bit more obvious that the helpers implement newer versions ofkvm_arch_vcpu_ioctl_set_sregs().No functional change intended.
Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/kvm/x86.c +0
-0@@ -133,8 +133,10 @@ static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu);
Quoted 10 lines
arch/x86/kvm/x86.c static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); · line 140
Quoted 7 lines
arch/x86/kvm/x86.c +4
-2-static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); -static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); +static int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2); +static void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2);Existing code uses the pattern
kvm_vcpu_ioctl_x86_xxx, is it better to align the pattern?
Yeah, good call, I'll use kvm_vcpu_ioctl_x86_xxx. In find the effective namespace to be a bit odd, but that's not a good reason for these to be different.
Sean Christopherson
maintainerPATCH v4 4/30 KVM: x86: Move the bulk of register specific code from x86.c to regs.c
In reply to Sean ChristophersonIntroduce regs.c, and move the vast majority of register specific code out of x86.c and into regs.c. Deliberately leave behind MSR code, as KVM's MSR support is complex enough to warrant its own compilation unit, and doesn't have much in common with the other register code.
Note, "struct kvm_sregs" has fields for EFER and MSR_IA32_APICBASE, and so the {G,S}ET_REGS flows technically contain a tiny amount of MSR code. MSR_IA32_APICBASE is already managed by lapic.c, and so doesn't require a "placement decision". As for EFER, leave all other EFER handling in x86.c (later to be moved to msrs.c). The primary interface to EFER, set_efer(), is very much MSR specific, even though EFER is arguably more of a Control Register than an MSR.
No functional change intended.
--- arch/x86/kvm/Makefile | 4 +- arch/x86/kvm/regs.c | 875 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/regs.h | 25 ++ arch/x86/kvm/x86.c | 887 +----------------------------------------- arch/x86/kvm/x86.h | 2 + 5 files changed, 906 insertions(+), 887 deletions(-) create mode 100644 arch/x86/kvm/regs.c
arch/x86/kvm/Makefile +2 -2
@@ -5,8 +5,8 @@ ccflags-$(CONFIG_KVM_WERROR) += -Werror include $(srctree)/virt/kvm/Makefile.kvm -kvm-y += x86.o emulate.o irq.o lapic.o cpuid.o pmu.o mtrr.o \ - debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o +kvm-y += x86.o emulate.o irq.o lapic.o cpuid.o pmu.o regs.o \ + mtrr.o debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o kvm-$(CONFIG_KVM_IOAPIC) += i8259.o i8254.o ioapic.o
arch/x86/kvm/regs.c new file +875 -0
875 lines not on this page · raw message
arch/x86/kvm/regs.h +25 -0
@@ -389,6 +389,14 @@ static inline bool kvm_dr6_valid(u64 data)return !(data >>32 ); } +static inline unsigned long kvm_get_effective_dr7 (struct kvm_vcpu *vcpu) +{ +if (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) +return vcpu->arch.guest_debug_dr7; + +return vcpu->arch.dr7; +} +static inline void enter_guest_mode (struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ;
@@ -417,4 +425,21 @@ static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg) return kvm_x86_call(get_segment_base)(vcpu, seg); } +void __kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags); + +void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2); +int kvm_x86_vcpu_ioctl_set_sregs2 (struct kvm_vcpu *vcpu, +struct kvm_sregs2 *sregs2); + +void kvm_run_sync_regs_to_user (struct kvm_vcpu *vcpu); +int kvm_run_sync_regs_from_user (struct kvm_vcpu *vcpu); + +void kvm_update_dr0123 (struct kvm_vcpu *vcpu); +int kvm_vcpu_ioctl_x86_get_debugregs (struct kvm_vcpu *vcpu, +struct kvm_debugregs *dbgregs); +int kvm_vcpu_ioctl_x86_set_debugregs (struct kvm_vcpu *vcpu, +struct kvm_debugregs *dbgregs); + +#endif
arch/x86/kvm/x86.c +2 -885
956 lines not on this page · raw message
arch/x86/kvm/x86.h +2 -0
@@ -403,6 +403,7 @@ int handle_ud(struct kvm_vcpu *vcpu);void kvm_deliver_exception_payload (struct kvm_vcpu *vcpu,struct kvm_queued_exception *ex); +void kvm_handle_exception_payload_quirk (struct kvm_vcpu *vcpu);int kvm_mtrr_set_msr (struct kvm_vcpu *vcpu,u32 msr,u64 data);int kvm_mtrr_get_msr (struct kvm_vcpu *vcpu,u32 msr,u64 *pdata);
@@ -597,6 +598,7 @@ static inline void kvm_machine_check(void)int kvm_spec_ctrl_test_value (u64 value);int kvm_handle_memory_failure (struct kvm_vcpu *vcpu,int r,struct x86_exception *e); +void kvm_invalidate_pcid (struct kvm_vcpu *vcpu,unsigned long pcid);int kvm_handle_invpcid (struct kvm_vcpu *vcpu,unsigned long type,gva_t gva);bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 04/30] KVM: x86: Move the bulk of register specific code from x86.c to regs.c
Quoted 7 lines · Sean Christopherson
Introduce
regs.c, and move the vast majority of register specific code out ofx86.cand intoregs.c. Deliberately leave behind MSR code, as KVM's MSR support is complex enough to warrant its own compilation unit, and doesn't have much in common with the other register code.Note, "struct
kvm_sregs" has fields for EFER andMSR_IA32_APICBASE, and so the {G,S}ET_REGS flows technically contain a tiny amount of MSR code.
^
Should be {G,S}ET_SREGS
Quoted 10 lines
MSR_IA32_APICBASEis already managed bylapic.c, and so doesn't require a "placement decision". As for EFER, leave all other EFER handling inx86.c(later to be moved tomsrs.c). The primary interface to EFER,set_efer(), is very much MSR specific, even though EFER is arguably more of a Control Register than an MSR.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 5/30 KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
In reply to Sean ChristophersonMove single-use local APIC IRQ helpers out of asm/kvm_host.h so that they are co-located with their user, and not exposed to the broader world.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 12 ------------ arch/x86/kvm/irq.c | 7 +++++++ arch/x86/kvm/lapic.h | 5 +++++ 3 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -12
@@ -1772,11 +1772,6 @@ struct kvm_lapic_irq {bool msi_redir_hint; }; -static inline u16 kvm_lapic_irq_dest_mode (bool dest_mode_logical) -{ -return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; -} -enum kvm_x86_run_flags {KVM_RUN_FORCE_IMMEDIATE_EXIT =BIT (0 ),KVM_RUN_LOAD_GUEST_DR6 =BIT (1 ),
@@ -2509,13 +2504,6 @@ void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu); -static inline bool kvm_irq_is_postable (struct kvm_lapic_irq *irq) -{ -/* We can only post Fixed and LowPrio IRQs */ -return (irq->delivery_mode ==APIC_DM_FIXED || - irq->delivery_mode ==APIC_DM_LOWEST ); -} -static inline void kvm_arch_vcpu_blocking (struct kvm_vcpu *vcpu) {kvm_x86_call (vcpu_blocking)(vcpu);
arch/x86/kvm/irq.c +7 -0
@@ -423,6 +423,13 @@ void kvm_arch_irq_routing_update(struct kvm *kvm)kvm_make_scan_ioapic_request (kvm); } +static bool kvm_irq_is_postable (struct kvm_lapic_irq *irq) +{ +/* We can only post Fixed and LowPrio IRQs */ +return (irq->delivery_mode ==APIC_DM_FIXED || + irq->delivery_mode ==APIC_DM_LOWEST ); +} +static int kvm_pi_update_irte (struct kvm_kernel_irqfd *irqfd,struct kvm_kernel_irq_routing_entry *entry) {
arch/x86/kvm/lapic.h +5 -0
@@ -237,6 +237,11 @@ static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)return lapic_in_kernel (vcpu) &&test_bit (KVM_APIC_INIT , &vcpu->arch.apic->pending_events); } +static inline u16 kvm_lapic_irq_dest_mode (bool dest_mode_logical) +{ +return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; +} +bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector);bool kvm_lapic_suppress_eoi_broadcast (struct kvm_lapic *apic);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 05/30] KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
Quoted 1 line · Sean Christopherson
Move single-use local APIC IRQ helpers out of
asm/kvm_host.hso that they
Nit:
Not sure if it's accurate to say kvm_lapic_irq_dest_mode() is a single-use helper.
Quoted 7 lines
are co-located with their user, and not exposed to the broader world.
No functional change intended.
Reviewed-by Kai Huang <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 70 lines
--- arch/x86/include/asm/kvm_host.h | 12 ------------ arch/x86/kvm/irq.c | 7 +++++++ arch/x86/kvm/lapic.h | 5 +++++ 3 files changed, 12 insertions(+), 12 deletions(-)arch/x86/include/asm/kvm_host.h +0
-12@@ -1772,11 +1772,6 @@ struct kvm_lapic_irq {bool msi_redir_hint; }; -static inline u16 kvm_lapic_irq_dest_mode (bool dest_mode_logical) -{ -return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; -} -enum kvm_x86_run_flags {KVM_RUN_FORCE_IMMEDIATE_EXIT =BIT (0 ),KVM_RUN_LOAD_GUEST_DR6 =BIT (1 ),@@ -2509,13 +2504,6 @@ void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu); -static inline bool kvm_irq_is_postable (struct kvm_lapic_irq *irq) -{ -/* We can only post Fixed and LowPrio IRQs */ -return (irq->delivery_mode ==APIC_DM_FIXED || - irq->delivery_mode ==APIC_DM_LOWEST ); -} -static inline void kvm_arch_vcpu_blocking (struct kvm_vcpu *vcpu) {kvm_x86_call (vcpu_blocking)(vcpu);arch/x86/kvm/irq.c +7
-0@@ -423,6 +423,13 @@ void kvm_arch_irq_routing_update(struct kvm *kvm)kvm_make_scan_ioapic_request (kvm); } +static bool kvm_irq_is_postable (struct kvm_lapic_irq *irq) +{ +/* We can only post Fixed and LowPrio IRQs */ +return (irq->delivery_mode ==APIC_DM_FIXED || + irq->delivery_mode ==APIC_DM_LOWEST ); +} +static int kvm_pi_update_irte (struct kvm_kernel_irqfd *irqfd,struct kvm_kernel_irq_routing_entry *entry) {arch/x86/kvm/lapic.h +5
-0@@ -237,6 +237,11 @@ static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)return lapic_in_kernel (vcpu) &&test_bit (KVM_APIC_INIT , &vcpu->arch.apic->pending_events); } +static inline u16 kvm_lapic_irq_dest_mode (bool dest_mode_logical) +{ +return dest_mode_logical ?APIC_DEST_LOGICAL :APIC_DEST_PHYSICAL ; +} +bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector);bool kvm_lapic_suppress_eoi_broadcast (struct kvm_lapic *apic);
Sean Christopherson
maintainerRe: [PATCH v4 05/30] KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
Quoted 6 lines · Binbin Wu
Quoted 1 line · Sean Christopherson
Move single-use local APIC IRQ helpers out of
asm/kvm_host.hso that theyNit:
Not sure if it's accurate to saykvm_lapic_irq_dest_mode()is a single-use helper.
Yeah, I'll rephrase the changelog. No idea what happened, maybe I wrote the changelog without looking ioapic.c code?
Sean Christopherson
maintainerPATCH v4 6/30 KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
In reply to Sean ChristophersonRelocate the kvm_caps and kvm_host_values struct definitions and their associated global variable declarations to asm/kvm_host.h to allow for a variety of cleanups in x86.h and mmu.h, and to establish a (hopefully) maintainable rule that asm/kvm_host.h's role is to define common structures (and declare any associated globals), and anything needed by arch-neutral KVM.
While it would be lovely to trim kvm_host.h down to the point where it *only* holds things needed by arch-neutral and/or non-KVM code, multiple attempts to do just that have failed miserably. Trying to "hide" code from arch-neutral KVM is too restrictive (and ultimately pointless), and KVM x86 itself also needs a place to define common structures and their globals, e.g. to avoid inconsistent header include chains and/or misplaced helpers.
E.g. as pointed out by Kai, it's weird that x86.h, which is a kitchen sink of sorts, includes regs.h, but not mmu.h. Literally the only reason that x86.h doesn't include mmu.h is that mmu.h references kvm_host, which is currently defined in x86.h. As a result of odd include ordering, the very clearly MMU-specific helper mmu_is_nested() lives in x86.h, not mmu.h
"Fix" the kvm_host dependency so that x86.h can be the "central" include everyone expects it to be, and set KVM x86 on the path to having somewhat sensible "rules" for what goes where:
- asm/kvm_host.h holds "common" structure definitions and associated key global variables, and things that are referenced by arch-neutral KVM.
- <thing>.{c,h} holds relevant declarations and definitions.
- x86.{c,h} is the kitchen sink for everything else.
--- arch/x86/include/asm/kvm_host.h | 44 ++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 45 --------------------------------- 2 files changed, 44 insertions(+), 45 deletions(-)
arch/x86/include/asm/kvm_host.h +44 -0
@@ -315,6 +315,50 @@ enum x86_intercept_stage;struct kvm_kernel_irqfd ;struct kvm_kernel_irq_routing_entry ; +struct kvm_caps { +/* control of guest tsc rate supported? */ +bool has_tsc_control; +/* maximum supported tsc_khz for guests */ +u32 max_guest_tsc_khz; +/* number of bits of the fractional part of the TSC scaling ratio */ +u8 tsc_scaling_ratio_frac_bits; +/* maximum allowed value of TSC scaling ratio */ +u64 max_tsc_scaling_ratio; +/* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */ +u64 default_tsc_scaling_ratio; +/* bus lock detection supported? */ +bool has_bus_lock_exit; +/* notify VM exit supported? */ +bool has_notify_vmexit; +/* bit mask of VM types */ +u32 supported_vm_types; + +u64 supported_mce_cap; +u64 supported_xcr0; +u64 supported_xss; +u64 supported_perf_cap; + +u64 supported_quirks; +u64 inapplicable_quirks; +}; +extern struct kvm_caps kvm_caps; + +struct kvm_host_values { +/* +* The host's raw MAXPHYADDR, i.e. the number of non-reserved physical +* address bits irrespective of features that repurpose legal bits, +* e.g. MKTME. +*/ +u8 maxphyaddr; + +u64 efer; +u64 xcr0; +u64 xss; +u64 s_cet; +u64 arch_capabilities; +}; +extern struct kvm_host_values kvm_host; +/* * kvm_mmu_page_role tracks the properties of a shadow page (where shadow page * also includes TDP pages) to determine whether or not a page can be used in
arch/x86/kvm/x86.h +0 -45
57 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
Quoted 21 lines · Sean Christopherson
Relocate the
kvm_capsandkvm_host_valuesstruct definitions and their associated global variable declarations toasm/kvm_host.hto allow for a variety of cleanups inx86.handmmu.h, and to establish a (hopefully) maintainable rule that asm/kvm_host.h's role is to define common structures (and declare any associated globals), and anything needed by arch-neutral KVM.While it would be lovely to trim
kvm_host.hdown to the point where it *only* holds things needed by arch-neutral and/or non-KVM code, multiple attempts to do just that have failed miserably. Trying to "hide" code from arch-neutral KVM is too restrictive (and ultimately pointless), and KVM x86 itself also needs a place to define common structures and their globals, e.g. to avoid inconsistent header include chains and/or misplaced helpers.E.g. as pointed out by Kai, it's weird that
x86.h, which is a kitchen sink of sorts, includesregs.h, but notmmu.h. Literally the only reason thatx86.hdoesn't includemmu.his thatmmu.hreferenceskvm_host, which is currently defined inx86.h. As a result of odd include ordering, the very clearly MMU-specific helpermmu_is_nested()lives inx86.h, notmmu.h
Quoted 6 lines
"Fix" the
kvm_hostdependency so thatx86.hcan be the "central" include everyone expects it to be, and set KVM x86 on the path to having somewhat sensible "rules" for what goes where:-
asm/kvm_host.hholds "common" structure definitions and associated key global variables, and things that are referenced by arch-neutral KVM.
I'm confused by the term "arch-neutral" all over the changelog. I suppose include/linux/kvm_host.h is arch-neutral while asm/kvm_host.h is arch-specific but not KVM internal only.
Quoted 2 lines
- <thing>.{c,h} holds relevant declarations and definitions.
- x86.{c,h} is the kitchen sink for everything else.
Re: [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
Quoted 22 lines · Xiaoyao Li
Quoted 21 lines · Sean Christopherson
Relocate the
kvm_capsandkvm_host_valuesstruct definitions and their associated global variable declarations toasm/kvm_host.hto allow for a variety of cleanups inx86.handmmu.h, and to establish a (hopefully) maintainable rule that asm/kvm_host.h's role is to define common structures (and declare any associated globals), and anything needed by arch-neutral KVM.While it would be lovely to trim
kvm_host.hdown to the point where it *only* holds things needed by arch-neutral and/or non-KVM code, multiple attempts to do just that have failed miserably. Trying to "hide" code from arch-neutral KVM is too restrictive (and ultimately pointless), and KVM x86 itself also needs a place to define common structures and their globals, e.g. to avoid inconsistent header include chains and/or misplaced helpers.E.g. as pointed out by Kai, it's weird that
x86.h, which is a kitchen sink of sorts, includesregs.h, but not mmu.h. Literally the only reason thatx86.hdoesn't includemmu.his thatmmu.hreferenceskvm_host, which is currently defined in x86.h. As a result of odd include ordering, the very clearly MMU-specific helpermmu_is_nested()lives inx86.h, notmmu.h
Quoted 8 lines
Quoted 6 lines
"Fix" the
kvm_hostdependency so thatx86.hcan be the "central" include everyone expects it to be, and set KVM x86 on the path to having somewhat sensible "rules" for what goes where:-
asm/kvm_host.hholds "common" structure definitions and associated key global variables, and things that are referenced by arch-neutral KVM.I'm confused by the term "arch-neutral" all over the changelog. I suppose
include/linux/kvm_host.his arch-neutral whileasm/kvm_host.his arch-specific but not KVM internal only.
IIUC, it's the situation where asm/kvm_host.h for x86 provides the x86-specific pieces that the generic/arch-neutral KVM depends on. E.g. struct kvm_arch is defined in asm/kvm_host.h, which is x86-specific, but the generic KVM embeds it in struct kvm as 'struct kvm_arch arch' in include/linux/kvm_host.h, i.e. "referenced by arch-neutral KVM".
Quoted 4 lines
Quoted 3 lines
- <thing>.{c,h} holds relevant declarations and definitions.
- x86.{c,h} is the kitchen sink for everything else.
Sean Christopherson
maintainerRe: [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
Quoted 23 lines · Binbin Wu
Quoted 22 lines · Xiaoyao Li
Quoted 21 lines · Sean Christopherson
Relocate the
kvm_capsandkvm_host_valuesstruct definitions and their associated global variable declarations toasm/kvm_host.hto allow for a variety of cleanups inx86.handmmu.h, and to establish a (hopefully) maintainable rule that asm/kvm_host.h's role is to define common structures (and declare any associated globals), and anything needed by arch-neutral KVM.While it would be lovely to trim
kvm_host.hdown to the point where it *only* holds things needed by arch-neutral and/or non-KVM code, multiple attempts to do just that have failed miserably. Trying to "hide" code from arch-neutral KVM is too restrictive (and ultimately pointless), and KVM x86 itself also needs a place to define common structures and their globals, e.g. to avoid inconsistent header include chains and/or misplaced helpers.E.g. as pointed out by Kai, it's weird that
x86.h, which is a kitchen sink of sorts, includesregs.h, but not mmu.h. Literally the only reason thatx86.hdoesn't includemmu.his thatmmu.hreferenceskvm_host, which is currently defined in x86.h. As a result of odd include ordering, the very clearly MMU-specific helpermmu_is_nested()lives inx86.h, notmmu.h
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
"Fix" the
kvm_hostdependency so thatx86.hcan be the "central" include everyone expects it to be, and set KVM x86 on the path to having somewhat sensible "rules" for what goes where:-
asm/kvm_host.hholds "common" structure definitions and associated key global variables, and things that are referenced by arch-neutral KVM.I'm confused by the term "arch-neutral" all over the changelog. I suppose
"arch-neutral" refers to virt/kvm code and other code that isn't arch specific. I used arch-neutral here, e.g. instead of "common code", to differentiate between arch-neutral (virt/kvm) code and common x86 (arch/x86/kvm) code.
Quoted 8 lines
Quoted 2 lines
include/linux/kvm_host.his arch-neutral whileasm/kvm_host.his arch-specific but not KVM internal only.IIUC, it's the situation where
asm/kvm_host.hfor x86 provides the x86-specific pieces that the generic/arch-neutral KVM depends on. E.g. structkvm_archis defined inasm/kvm_host.h, which is x86-specific, but the generic KVM embeds it in struct kvm as 'structkvm_archarch' ininclude/linux/kvm_host.h, i.e. "referenced by arch-neutral KVM".
Yep, exactly.
Re: [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
Quoted 28 lines · Sean Christopherson
Quoted 27 lines · Binbin Wu
Quoted 26 lines · Xiaoyao Li
Quoted 25 lines · Sean Christopherson
Relocate the
kvm_capsandkvm_host_valuesstruct definitions and their associated global variable declarations toasm/kvm_host.hto allow for a variety of cleanups inx86.handmmu.h, and to establish a (hopefully) maintainable rule that asm/kvm_host.h's role is to define common structures (and declare any associated globals), and anything needed by arch-neutral KVM.While it would be lovely to trim
kvm_host.hdown to the point where it *only* holds things needed by arch-neutral and/or non-KVM code, multiple attempts to do just that have failed miserably. Trying to "hide" code from arch-neutral KVM is too restrictive (and ultimately pointless), and KVM x86 itself also needs a place to define common structures and their globals, e.g. to avoid inconsistent header include chains and/or misplaced helpers.E.g. as pointed out by Kai, it's weird that
x86.h, which is a kitchen sink of sorts, includesregs.h, but not mmu.h. Literally the only reason thatx86.hdoesn't includemmu.his thatmmu.hreferenceskvm_host, which is currently defined in x86.h. As a result of odd include ordering, the very clearly MMU-specific helpermmu_is_nested()lives inx86.h, notmmu.h"Fix" the
kvm_hostdependency so thatx86.hcan be the "central" include everyone expects it to be, and set KVM x86 on the path to having somewhat sensible "rules" for what goes where:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
-
asm/kvm_host.hholds "common" structure definitions and associated key global variables, and things that are referenced by arch-neutral KVM.I'm confused by the term "arch-neutral" all over the changelog. I suppose
"arch-neutral" refers to
virt/kvmcode and other code that isn't arch specific. I used arch-neutral here, e.g. instead of "common code", to differentiate between arch-neutral (virt/kvm) code and common x86 (arch/x86/kvm) code.
I figure out why I got confused. I read it as the "that are referenced by arch-neutral KVM" was explaining the whole
"common" structure definitions and associated key global variables, and things
instead of only "things"
Quoted 10 lines
Quoted 8 lines
Quoted 2 lines
include/linux/kvm_host.his arch-neutral whileasm/kvm_host.his arch-specific but not KVM internal only.IIUC, it's the situation where
asm/kvm_host.hfor x86 provides the x86-specific pieces that the generic/arch-neutral KVM depends on. E.g. structkvm_archis defined inasm/kvm_host.h, which is x86-specific, but the generic KVM embeds it in struct kvm as 'structkvm_archarch' ininclude/linux/kvm_host.h, i.e. "referenced by arch-neutral KVM".Yep, exactly.
I get it now. Thanks for the explanation!
Sean Christopherson
maintainerPATCH v4 7/30 KVM: x86: Swap the include order between x86.h and mmu.h
In reply to Sean ChristophersonInvert the include ordering between x86.h and mmu.h, and move mmu_is_nested() to mmu.h where it belongs (mmu_is_nested()'s placement in x86.h was solely responsible for the existing ordering), so that x86.h is not included by most KVM internal headers (but includes them).
No functional change intended.
--- arch/x86/kvm/mmu.h | 6 +++++- arch/x86/kvm/x86.h | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu.h +5 -1
@@ -4,7 +4,6 @@#include <linux/kvm_host.h> #include "regs.h" -#include "x86.h" #include "cpuid.h" extern bool __read_mostly enable_mmio_caching;
@@ -300,6 +299,11 @@ static inline void kvm_update_page_stats(struct kvm *kvm, int level, int count)atomic64_add (count, &kvm->stat.pages[level -1 ]); } +static inline bool mmu_is_nested (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; +} + static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, gpa_t gpa, u64 access,
arch/x86/kvm/x86.h +1 -5
@@ -6,6 +6,7 @@#include <asm/fpu/xstate.h> #include <asm/mce.h> #include <asm/pvclock.h> +#include "mmu.h" #include "regs.h" #include "kvm_emulate.h" #include "cpuid.h"
@@ -210,11 +211,6 @@ static inline bool x86_exception_has_error_code(unsigned int vector)return (1U << vector) & exception_has_error_code; } -static inline bool mmu_is_nested (struct kvm_vcpu *vcpu) -{ -return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; -} -static inline u8 vcpu_virt_addr_bits (struct kvm_vcpu *vcpu) {return kvm_is_cr4_bit_set (vcpu,X86_CR4_LA57 ) ?57 :48 ;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 07/30] KVM: x86: Swap the include order between x86.h and mmu.h
Quoted 9 lines · Sean Christopherson
Invert the include ordering between
x86.handmmu.h, and movemmu_is_nested()tommu.hwhere it belongs (mmu_is_nested()'s placement inx86.hwas solely responsible for the existing ordering), so thatx86.his not included by most KVM internal headers (but includes them).No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 8/30 KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h
In reply to Sean ChristophersonRelocated the declaration of tdp_enabled into mmu.h, and opportunistically hoist tdp_mmu_enabled up to the top so that the two are co-located.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/mmu.h | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
@@ -2188,8 +2188,6 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3); -extern bool tdp_enabled; -/* * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing * userspace I/O) to indicate that the emulation context
arch/x86/kvm/mmu.h +6 -6
@@ -6,6 +6,12 @@#include "regs.h" #include "cpuid.h" +extern bool tdp_enabled; +#ifdef CONFIG_X86_64 +extern bool tdp_mmu_enabled; +#else +#define tdp_mmu_enabled false +#endif extern bool __read_mostly enable_mmio_caching;#define PT_WRITABLE_SHIFT 1
@@ -260,12 +266,6 @@ static inline bool kvm_shadow_root_allocated(struct kvm *kvm)return smp_load_acquire (&kvm->arch.shadow_root_allocated); } -#ifdef CONFIG_X86_64 -extern bool tdp_mmu_enabled; -#else -#define tdp_mmu_enabled false -#endif -int kvm_tdp_mmu_map_private_pfn (struct kvm_vcpu *vcpu,gfn_t gfn,kvm_pfn_t pfn);static inline bool kvm_memslots_have_rmaps (struct kvm *kvm)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 08/30] KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h
Quoted 1 line · Sean Christopherson
Relocated the declaration of
tdp_enabledintommu.h, and opportunistically
Nit:
Relocated -> Relocate
Quoted 7 lines
hoist
tdp_mmu_enabledup to the top so that the two are co-located.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 9/30 KVM: x86: Move eager_page_split to mmu.{c,h}
In reply to Sean ChristophersonMove KVM's eager_page_split module param to the MMU, as it is very much an MMU knob.
No functional change intended.
--- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +++ arch/x86/kvm/x86.c | 3 --- arch/x86/kvm/x86.h | 2 -- 4 files changed, 4 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.h +1 -0
@@ -13,6 +13,7 @@ extern bool tdp_mmu_enabled;#define tdp_mmu_enabled false#endif extern bool __read_mostly enable_mmio_caching; +extern bool eager_page_split;#define PT_WRITABLE_SHIFT 1#define PT_USER_SHIFT 2
arch/x86/kvm/mmu/mmu.c +3 -0
@@ -114,6 +114,9 @@ module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0444);EXPORT_SYMBOL_FOR_KVM_INTERNAL (tdp_mmu_enabled);#endif +bool __read_mostly eager_page_split = true; +module_param (eager_page_split, bool,0644 ); +static int max_huge_page_level__read_mostly ;static int tdp_root_level__read_mostly ;static int max_tdp_level__read_mostly ;
arch/x86/kvm/x86.c +0 -3
@@ -177,9 +177,6 @@ module_param(force_emulation_prefix, int, 0644);int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,0644 ); -bool __read_mostly eager_page_split = true; -module_param (eager_page_split, bool,0644 ); -/* Enable/disable SMT_RSB bug mitigation */ static bool __read_mostly mitigate_smt_rsb;module_param (mitigate_smt_rsb, bool,0444 );
arch/x86/kvm/x86.h +0 -2
@@ -415,8 +415,6 @@ extern int pi_inject_timer;extern bool report_ignored_msrs; -extern bool eager_page_split; -static inline void kvm_pr_unimpl_wrmsr (struct kvm_vcpu *vcpu,u32 msr,u64 data) {if (report_ignored_msrs)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 09/30] KVM: x86: Move eager_page_split to mmu.{c,h}
Quoted 8 lines · Sean Christopherson
Move KVM's
eager_page_splitmodule param to the MMU, as it is very much an MMU knob.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 12 lines
--- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 3 +++ arch/x86/kvm/x86.c | 3 --- arch/x86/kvm/x86.h | 2 -- 4 files changed, 4 insertions(+), 5 deletions(-)arch/x86/kvm/mmu.h +0
-0
arch/x86/kvm/mmu.h extern bool tdp_mmu_enabled; · line 16
Quoted 4 lines
arch/x86/kvm/mmu.h +1
-0@@ -13,6 +13,7 @@ extern bool tdp_mmu_enabled;#define tdp_mmu_enabled false#endif extern bool __read_mostly enable_mmio_caching; +extern bool eager_page_split;
Side topic, the attributes don't take effect for declarations, but in general, does KVM/kernel coding style have a preference to keep it for consistency/annotation or just drop it?
Sean Christopherson
maintainerRe: [PATCH v4 09/30] KVM: x86: Move eager_page_split to mmu.{c,h}
Quoted 5 lines · Binbin Wu
Quoted 5 lines
arch/x86/kvm/mmu.h +0
-0@@ -13,6 +13,7 @@ extern bool tdp_mmu_enabled;
Quoted 8 lines
arch/x86/kvm/mmu.h extern bool tdp_mmu_enabled; · line 16
Quoted 4 lines
arch/x86/kvm/mmu.h +1
-0#define tdp_mmu_enabled false#endif extern bool __read_mostly enable_mmio_caching; +extern bool eager_page_split;Side topic, the attributes don't take effect for declarations, but in general, does KVM/kernel coding style have a preference to keep it for consistency/annotation or just drop it?
No, not as far as I know. Oh, nice! I was going to say that I don't have a strong preference, because I assumed that conflicting annotations between the declaration and definition wouldn't be flagged by the compiler, i.e. that it'd be fairly easy to end up with stale annotations. But it turns out that's not true:
arch/x86/kvm/vmx/vmx.c:214:8: error: ignoring attribute ‘section (".data..ro_after_init")’
because it conflicts with previous ‘section (".data..read_mostly")’ [-Werror=attributes]
214 | struct x86_pmu_lbr __ro_after_init vmx_lbr_caps;
| ^~~~~~~~~~~
In file included from arch/x86/kvm/vmx/vmx.h:14,
from arch/x86/kvm/vmx/hyperv.h:7,
from arch/x86/kvm/vmx/vmx.c:59:
arch/x86/kvm/vmx/pmu_intel.h:41:27: note: previous declaration here
41 | extern struct x86_pmu_lbr vmx_lbr_caps __read_mostly;
| ^~~~~~~~~~~~So unless there's a wrinkle I'm missing, my preference would definitely be to annotate the declarations as well. More information is pretty much always going to be better in this case.
Sean Christopherson
maintainerPATCH v4 10/30 KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h
In reply to Sean ChristophersonDrop an mostly unused include of x86.h from hyperv.h, and instead pull in regs.h, which is need for at least is_guest_mode(). This eliminates the last include of x86.h from a common x86 header, i.e. solidifies that x86.h is the top of the pyramid.
Add a missing x86.h include in cpuid.c to avoid build breakage.
No functional change intended.
--- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/hyperv.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -0
@@ -28,6 +28,7 @@#include "trace.h" #include "pmu.h" #include "xen.h" +#include "x86.h" /* * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
arch/x86/kvm/hyperv.h +2 -1
@@ -22,7 +22,8 @@#define __ARCH_X86_KVM_HYPERV_H__#include <linux/kvm_host.h> -#include "x86.h" + +#include "regs.h" #ifdef CONFIG_KVM_HYPERV
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 10/30] KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h
Quoted 1 line · Sean Christopherson
Drop an mostly unused include of
x86.hfromhyperv.h, and instead pull in
^
a> regs.h, which is need for at least is_guest_mode(). This eliminates the
^
needed> last include of x86.h from a common x86 header, i.e. solidifies that x86.h
Quoted 8 lines
is the top of the pyramid.
Add a missing
x86.hinclude incpuid.cto avoid build breakage.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 31 lines
--- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/hyperv.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)arch/x86/kvm/cpuid.c +1
-0@@ -28,6 +28,7 @@#include "trace.h" #include "pmu.h" #include "xen.h" +#include "x86.h" /* * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be arch/x86/kvm/hyperv.h +2
-1@@ -22,7 +22,8 @@#define __ARCH_X86_KVM_HYPERV_H__#include <linux/kvm_host.h> -#include "x86.h" + +#include "regs.h" #ifdef CONFIG_KVM_HYPERV
Sean Christopherson
maintainerPATCH v4 11/30 KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
In reply to Sean ChristophersonMove the kvm_{load,put}_guest_fpu() helpers to fpu.h in anticipation of moving the bulk of KVM's register specific code out of x86.c.
No functional change intended.
--- arch/x86/kvm/fpu.h | 26 ++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 24 ------------------------ 2 files changed, 26 insertions(+), 24 deletions(-)
arch/x86/kvm/fpu.h +26 -0
@@ -3,8 +3,34 @@#ifndef __KVM_FPU_H_#define __KVM_FPU_H_ +#include <linux/kvm_host.h> + +#include <trace/events/kvm.h> +#include <asm/fpu/api.h> +/* Swap (qemu) user FPU context for the guest FPU context. */ +static inline void kvm_load_guest_fpu (struct kvm_vcpu *vcpu) +{ +if (KVM_BUG_ON (vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm)) +return ; + +/* Exclude PKRU, it's restored separately immediately after VM-Exit. */ +fpu_swap_kvm_fpstate (&vcpu->arch.guest_fpu, true); +trace_kvm_fpu (1 ); +} + +/* When vcpu_run ends, restore user space FPU context. */ +static inline void kvm_put_guest_fpu (struct kvm_vcpu *vcpu) +{ +if (KVM_BUG_ON (!vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm)) +return ; + +fpu_swap_kvm_fpstate (&vcpu->arch.guest_fpu, false); + ++vcpu->stat.fpu_reload; +trace_kvm_fpu (0 ); +} + typedef u32 __attribute__((vector_size(16))) sse128_t;#define __sse128_u union { sse128_t vec; u64 as_u64[2]; u32 as_u32[4]; }#define sse128_lo (x) ({ __sse128_u t; t.vec = x; t.as_u64[0]; })
arch/x86/kvm/x86.c +0 -24
@@ -133,8 +133,6 @@ static void store_regs(struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu);static DEFINE_MUTEX (vendor_module_lock); -static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu); -static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu);struct kvm_x86_ops kvm_x86_ops__read_mostly ;
@@ -11432,28 +11430,6 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu)return 0 ; } -/* Swap (qemu) user FPU context for the guest FPU context. */ -static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu) -{ -if (KVM_BUG_ON (vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm)) -return ; - -/* Exclude PKRU, it's restored separately immediately after VM-Exit. */ -fpu_swap_kvm_fpstate (&vcpu->arch.guest_fpu, true); -trace_kvm_fpu (1 ); -} - -/* When vcpu_run ends, restore user space FPU context. */ -static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu) -{ -if (KVM_BUG_ON (!vcpu->arch.guest_fpu.fpstate->in_use, vcpu->kvm)) -return ; - -fpu_swap_kvm_fpstate (&vcpu->arch.guest_fpu, false); - ++vcpu->stat.fpu_reload; -trace_kvm_fpu (0 ); -} -static int kvm_x86_vcpu_pre_run (struct kvm_vcpu *vcpu) {/*
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 11/30] KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
Quoted 2 lines · Sean Christopherson
Move the kvm_{load,put}_guest_fpu() helpers to
fpu.hin anticipation of moving the bulk of KVM's register specific code out ofx86.c.
Nit:
I think patch 4 did the moving, so it's not "in anticipation of" anymore?
Quoted 6 lines
No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v4 11/30] KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
Quoted 6 lines · Binbin Wu
Quoted 2 lines · Sean Christopherson
Move the kvm_{load,put}_guest_fpu() helpers to
fpu.hin anticipation of moving the bulk of KVM's register specific code out ofx86.c.Nit:
I think patch 4 did the moving, so it's not "in anticipation of" anymore?
Oh, nice catch. It's a typo, it should be MSR, not register. I'll change to this:
Move the kvm_{load,put}_guest_fpu() helpers to fpu.h in anticipation of moving the bulk of KVM's MSR specific code out of x86.c (KVM needs to load and put the FPU when accessing MSRs that are managed via XSTATE, a.k.a. the so called "FPU").
Sean Christopherson
maintainerPATCH v4 12/30 KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
In reply to Sean ChristophersonExtract the code for getting/setting MSRs and MSR lists to dedicated helpers in anticipation of moving the MSR code to a new msrs.c.
No functional change intended.
--- arch/x86/kvm/x86.c | 135 ++++++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 57 deletions(-)
arch/x86/kvm/x86.c +78 -57
@@ -4602,6 +4602,61 @@ static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr)return __kvm_x86_dev_get_attr (attr, &val); } +static int kvm_get_msr_index_list (struct kvm_msr_list __user *user_msr_list) +{ +struct kvm_msr_list msr_list; +unsigned int n; + +if (copy_from_user (&msr_list, user_msr_list,sizeof (msr_list))) +return -EFAULT ; + + n = msr_list.nmsrs; + msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; +if (copy_to_user (user_msr_list, &msr_list,sizeof (msr_list))) +return -EFAULT ; + +if (n < msr_list.nmsrs) +return -E2BIG ; + +if (copy_to_user (user_msr_list->indices, &msrs_to_save, + num_msrs_to_save *sizeof (u32))) +return -EFAULT ; + +if (copy_to_user (user_msr_list->indices + num_msrs_to_save, + &emulated_msrs, num_emulated_msrs *sizeof (u32))) +return -EFAULT ; + +return 0 ; +} + +static int kvm_get_feature_msr_index_list (struct kvm_msr_list __user *user_msr_list) +{ +struct kvm_msr_list msr_list; +unsigned int n; + +if (copy_from_user (&msr_list, user_msr_list,sizeof (msr_list))) +return -EFAULT ; + + n = msr_list.nmsrs; + msr_list.nmsrs = num_msr_based_features; +if (copy_to_user (user_msr_list, &msr_list,sizeof (msr_list))) +return -EFAULT ; + +if (n < msr_list.nmsrs) +return -E2BIG ; + +if (copy_to_user (user_msr_list->indices, &msr_based_features, + num_msr_based_features *sizeof (u32))) +return -EFAULT ; + +return 0 ; +} + +static int kvm_get_feature_msrs (struct kvm_msrs __user *user_msrs) +{ +return msr_io (NULL , user_msrs, do_get_feature_msr,1 ); +} +long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {
@@ -4609,32 +4664,9 @@ long kvm_arch_dev_ioctl(struct file *filp,long r;switch (ioctl) { -case KVM_GET_MSR_INDEX_LIST : { -struct kvm_msr_list __user *user_msr_list = argp; -struct kvm_msr_list msr_list; -unsigned n; - - r = -EFAULT ; -if (copy_from_user (&msr_list, user_msr_list,sizeof (msr_list))) - goto out; - n = msr_list.nmsrs; - msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; -if (copy_to_user (user_msr_list, &msr_list,sizeof (msr_list))) - goto out; - r = -E2BIG ; -if (n < msr_list.nmsrs) - goto out; - r = -EFAULT ; -if (copy_to_user (user_msr_list->indices, &msrs_to_save, - num_msrs_to_save *sizeof (u32))) - goto out; -if (copy_to_user (user_msr_list->indices + num_msrs_to_save, - &emulated_msrs, - num_emulated_msrs *sizeof (u32))) - goto out; - r =0 ; +case KVM_GET_MSR_INDEX_LIST : + r =kvm_get_msr_index_list (argp);break ; - }case KVM_GET_SUPPORTED_CPUID :case KVM_GET_EMULATED_CPUID : {struct kvm_cpuid2 __user *cpuid_arg = argp;
@@ -4662,30 +4694,11 @@ long kvm_arch_dev_ioctl(struct file *filp, goto out; r =0 ;break ; -case KVM_GET_MSR_FEATURE_INDEX_LIST : { -struct kvm_msr_list __user *user_msr_list = argp; -struct kvm_msr_list msr_list; -unsigned int n; - - r = -EFAULT ; -if (copy_from_user (&msr_list, user_msr_list,sizeof (msr_list))) - goto out; - n = msr_list.nmsrs; - msr_list.nmsrs = num_msr_based_features; -if (copy_to_user (user_msr_list, &msr_list,sizeof (msr_list))) - goto out; - r = -E2BIG ; -if (n < msr_list.nmsrs) - goto out; - r = -EFAULT ; -if (copy_to_user (user_msr_list->indices, &msr_based_features, - num_msr_based_features *sizeof (u32))) - goto out; - r =0 ; +case KVM_GET_MSR_FEATURE_INDEX_LIST : + r =kvm_get_feature_msr_index_list (argp);break ; - }case KVM_GET_MSRS : - r =msr_io (NULL , argp, do_get_feature_msr,1 ); + r =kvm_get_feature_msrs (argp);break ;#ifdef CONFIG_KVM_HYPERV case KVM_GET_SUPPORTED_HV_CPUID :
@@ -5719,6 +5732,20 @@ static int kvm_get_reg_list(struct kvm_vcpu *vcpu,return 0 ; } +static int kvm_get_msrs (struct kvm_vcpu *vcpu,struct kvm_msrs __user *user_msrs) +{ +guard (srcu)(&vcpu->kvm->srcu); + +return msr_io (vcpu, user_msrs, do_get_msr,1 ); +} + +static int kvm_set_msrs (struct kvm_vcpu *vcpu,struct kvm_msrs __user *user_msrs) +{ +guard (srcu)(&vcpu->kvm->srcu); + +return msr_io (vcpu, user_msrs, do_set_msr,0 ); +} +long kvm_arch_vcpu_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {
@@ -5823,18 +5850,12 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r =0 ;break ; } -case KVM_GET_MSRS : { -int idx =srcu_read_lock (&vcpu->kvm->srcu); - r =msr_io (vcpu, argp, do_get_msr,1 ); -srcu_read_unlock (&vcpu->kvm->srcu, idx); +case KVM_GET_MSRS : + r =kvm_get_msrs (vcpu, argp);break ; - } -case KVM_SET_MSRS : { -int idx =srcu_read_lock (&vcpu->kvm->srcu); - r =msr_io (vcpu, argp, do_set_msr,0 ); -srcu_read_unlock (&vcpu->kvm->srcu, idx); +case KVM_SET_MSRS : + r =kvm_set_msrs (vcpu, argp);break ; - }case KVM_GET_ONE_REG :case KVM_SET_ONE_REG : r =kvm_get_set_one_reg (vcpu, ioctl, argp);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 12/30] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
Quoted 8 lines · Sean Christopherson
Extract the code for getting/setting MSRs and MSR lists to dedicated helpers in anticipation of moving the MSR code to a new
msrs.c.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 13/30 KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
In reply to Sean ChristophersonBegrudgingly move adjust_tsc_offset_{guest,host}() to x86.h as inlines, and expose several other TSC helpers in anticipation of moving KVM's MSR code to a dedicated msrs.c. Unfortunately for KVM, several MSRs that KVM emulates can affect TSC state.
Opportunistically drop a superfluous local "tsc_offset" variable, whose existence causes checkpatch to complain about lack of a blank line.
No functional change intended.
--- arch/x86/kvm/x86.c | 22 +++------------------- arch/x86/kvm/x86.h | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-)
arch/x86/kvm/x86.c +3 -19
@@ -2225,7 +2225,7 @@ u64 kvm_scale_tsc(u64 tsc, u64 ratio)return _tsc; } -static u64 kvm_compute_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc) +u64 kvm_compute_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc) {u64 tsc;
@@ -2266,7 +2266,7 @@ u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_calc_nested_tsc_multiplier); -static void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 l1_offset) +void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 l1_offset) {if (vcpu->arch.guest_tsc_protected)return ;
@@ -2380,7 +2380,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,kvm_track_tsc_matching (vcpu, !matched); } -static void kvm_synchronize_tsc (struct kvm_vcpu *vcpu,u64 *user_value) +void kvm_synchronize_tsc (struct kvm_vcpu *vcpu,u64 *user_value) {u64 data = user_value ? *user_value :0 ;struct kvm *kvm = vcpu->kvm;
@@ -2448,22 +2448,6 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value)raw_spin_unlock_irqrestore (&kvm->arch.tsc_write_lock, flags); } -static inline void adjust_tsc_offset_guest (struct kvm_vcpu *vcpu, -s64 adjustment) -{ -u64 tsc_offset = vcpu->arch.l1_tsc_offset; -kvm_vcpu_write_tsc_offset (vcpu, tsc_offset + adjustment); -} - -static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) -{ -if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) -WARN_ON (adjustment <0 ); - adjustment =kvm_scale_tsc ((u64 ) adjustment, - vcpu->arch.l1_tsc_scaling_ratio); -adjust_tsc_offset_guest (vcpu, adjustment); -} -#ifdef CONFIG_X86_64 static u64 read_tsc (void )
arch/x86/kvm/x86.h +19 -0
@@ -345,6 +345,25 @@ uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm);bool kvm_get_monotonic_and_clockread (s64 *kernel_ns,u64 *tsc_timestamp);int kvm_guest_time_update (struct kvm_vcpu *v); +void kvm_synchronize_tsc (struct kvm_vcpu *vcpu,u64 *user_value); +u64 kvm_compute_l1_tsc_offset (struct kvm_vcpu *vcpu,u64 target_tsc); +void kvm_vcpu_write_tsc_offset (struct kvm_vcpu *vcpu,u64 l1_offset); + +static inline void adjust_tsc_offset_guest (struct kvm_vcpu *vcpu, +s64 adjustment) +{ +kvm_vcpu_write_tsc_offset (vcpu, vcpu->arch.l1_tsc_offset + adjustment); +} + +static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) +{ +if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) +WARN_ON (adjustment <0 ); + adjustment =kvm_scale_tsc ((u64 ) adjustment, + vcpu->arch.l1_tsc_scaling_ratio); +adjust_tsc_offset_guest (vcpu, adjustment); +} +int kvm_read_guest_virt (struct kvm_vcpu *vcpu,gva_t addr,void *val,unsigned int bytes,struct x86_exception *exception);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v4 13/30] KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Lockless iteration of vm_list in kvm_arch_enable_virtualization_cpu during CPU hotplug.
- [High] Lockless cross-CPU modification of vcpu->arch.tsc_offset_adjustment.
--
commit ab7eedcd233e127b5aac182b220a36fa043a7767
Author: Sean Christopherson <[email protected]> Date: Sat Jun 13 00:03:12 2026 +0000
KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
Begrudgingly move adjust_tsc_offset_{guest,host}() to x86.h as inlines, and expose several other TSC helpers in anticipation of moving KVM's MSR code to a dedicated msrs.c. Unfortunately for KVM, several MSRs that KVM emulates can affect TSC state.
Opportunistically drop a superfluous local "tsc_offset" variable, whose existence causes checkpatch to complain about lack of a blank line.
No functional change intended.
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0
[ ... ]
Quoted 13 lines
arch/x86/kvm/x86.c +0
-9@@ -2448,22 +2448,6 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value)raw_spin_unlock_irqrestore (&kvm->arch.tsc_write_lock, flags); } -static inline void adjust_tsc_offset_guest (struct kvm_vcpu *vcpu, -s64 adjustment) -{ -u64 tsc_offset = vcpu->arch.l1_tsc_offset; -kvm_vcpu_write_tsc_offset (vcpu, tsc_offset + adjustment); -} - -static inline void adjust_tsc_offset_host (struct kvm_vcpu *vcpu,s64 adjustment) -{
arch/x86/kvm/x86.c static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value) · line 2453
Quoted 10 lines
arch/x86/kvm/x86.c +0
-7-if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) -WARN_ON (adjustment <0 ); - adjustment =kvm_scale_tsc ((u64 ) adjustment, - vcpu->arch.l1_tsc_scaling_ratio); -adjust_tsc_offset_guest (vcpu, adjustment); -} -#ifdef CONFIG_X86_64 static u64 read_tsc (void )
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing these TSC offset adjustments I noticed a race condition in how vcpu->arch.tsc_offset_adjustment is updated during CPU hotplug.
In kvm_arch_enable_virtualization_cpu(), there is a lockless cross-CPU modification of vcpu->arch.tsc_offset_adjustment:
arch/x86/kvm/x86.c:kvm_arch_enable_virtualization_cpu()
if (backwards_tsc) {
u64 delta_cyc = max_tsc - local_tsc;
list_for_each_entry(kvm, &vm_list, vm_list) {
kvm->arch.backwards_tsc_observed = true;
kvm_for_each_vcpu(i, vcpu, kvm) {
vcpu->arch.tsc_offset_adjustment += delta_cyc;Since CPU hotplug only pauses the CPU being brought online, could the targeted VCPU be concurrently executing kvm_arch_vcpu_load() on another CPU?
If kvm_arch_vcpu_load() reads and clears this field concurrently without vcpu->mutex, could the adjustment be lost or applied twice and lead to incorrect guest TSC values?
[Severity: High]
This is a pre-existing issue, but I also noticed that the iteration over vm_list in kvm_arch_enable_virtualization_cpu() is lockless:
arch/x86/kvm/x86.c:kvm_arch_enable_virtualization_cpu()
list_for_each_entry(kvm, &vm_list, vm_list) {
kvm_for_each_vcpu(i, vcpu, kvm) {If a user concurrently creates or destroys a VM (which modifies vm_list under kvm_lock), couldn't this lockless list_for_each_entry() dereference poisoned pointers from list_del() during destruction, leading to a kernel panic?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Sean Christopherson
maintainerPATCH v4 14/30 KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h}
In reply to Sean ChristophersonIntroduce msrs.{c,h}, and move the vast majority of MSR specific code out of x86.{c,h}. Use a plural "msrs" instead of just "msr" to be consistent with regs.{c,h}, and to make it easier to differentiate KVM's code from the other 5+ msr.c files in the kernel.
Opportunistically drop the "x86.h" include from mtrr.c, mostly as proof that the bulk of the MSR code is indeed being relocated to msrs.c.
No functional change intended.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/msrs.c | 2732 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/msrs.h | 128 ++ arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/x86.c | 2710 +--------------------------------------- arch/x86/kvm/x86.h | 87 +- 6 files changed, 2867 insertions(+), 2794 deletions(-) create mode 100644 arch/x86/kvm/msrs.c create mode 100644 arch/x86/kvm/msrs.h
arch/x86/kvm/Makefile +1 -1
@@ -5,7 +5,7 @@ ccflags-$(CONFIG_KVM_WERROR) += -Werror include $(srctree)/virt/kvm/Makefile.kvm -kvm-y += x86.o emulate.o irq.o lapic.o cpuid.o pmu.o regs.o \ +kvm-y += x86.o emulate.o irq.o lapic.o cpuid.o msrs.o pmu.o regs.o \ mtrr.o debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o
arch/x86/kvm/msrs.c new file +2732 -0
2,732 lines not on this page · raw message
arch/x86/kvm/msrs.h new file +128 -0
128 lines not on this page · raw message
arch/x86/kvm/mtrr.c +1 -1
@@ -19,7 +19,7 @@#include <asm/mtrr.h> #include "cpuid.h" -#include "x86.h" +#include "msrs.h" static u64 *find_mtrr (struct kvm_vcpu *vcpu,unsigned int msr) {
arch/x86/kvm/x86.c +3 -2707
124 lines not on this page · raw message
arch/x86/kvm/x86.h +2 -85
87 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Part of the diff is not shown here. View the raw message.
Re: [PATCH v4 14/30] KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h}
Quoted 5 lines · Sean Christopherson
Introduce msrs.{c,h}, and move the vast majority of MSR specific code out of x86.{c,h}. Use a plural "msrs" instead of just "msr" to be consistent with regs.{c,h}, and to make it easier to differentiate KVM's code from the other 5+
msr.cfiles in the kernel.
Quoted 7 lines
Opportunistically drop the "
x86.h" include frommtrr.c, mostly as proof that the bulk of the MSR code is indeed being relocated tomsrs.c.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
One nit below.
[...]> diff --git a/arch/x86/kvm/msrs.h b/arch/x86/kvm/msrs.h
arch/x86/kvm/msrs.h line 3
Quoted 8 lines
new file mode 100644
index 000000000000..25b75fe39b80arch/x86/kvm/msrs.h new file +3
-0@@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef ARCH_X86_KVM_MSR_H +#define ARCH_X86_KVM_MSR_H
To align with the file name,ARCH_X86_KVM_MSR_H -> ARCH_X86_KVM_MSRS_H ?
Sean Christopherson
maintainerPATCH v4 15/30 KVM: x86: Move register helper declarations from kvm_host.h => regs.h
In reply to Sean ChristophersonRelocate declarations of Control/Debug Register, EFLAGS and RIP helpers from x86's kvm_host.h to regs.h, to continue trimming down kvm_host.h.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 19 ------------------- arch/x86/kvm/regs.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 19 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -19
@@ -2186,8 +2186,6 @@ void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen);void kvm_mmu_change_mmu_pages (struct kvm *kvm,unsigned long kvm_nr_mmu_pages);void kvm_zap_gfn_range (struct kvm *kvm,gfn_t gfn_start,gfn_t gfn_end); -int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3); -/* * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing * userspace I/O) to indicate that the emulation context
@@ -2312,24 +2310,12 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); -void kvm_post_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0); -void kvm_post_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4); -int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0); -int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3); -int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); -int kvm_set_cr8 (struct kvm_vcpu *vcpu,unsigned long cr8); -int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val); -unsigned long kvm_get_dr (struct kvm_vcpu *vcpu,int dr); -unsigned long kvm_get_cr8 (struct kvm_vcpu *vcpu); -void kvm_lmsw (struct kvm_vcpu *vcpu,unsigned long msw);int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr);int kvm_emulate_xsetbv (struct kvm_vcpu *vcpu);int kvm_get_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr);int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); -unsigned long kvm_get_rflags (struct kvm_vcpu *vcpu); -void kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);int kvm_emulate_rdpmc (struct kvm_vcpu *vcpu);void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr);
@@ -2366,8 +2352,6 @@ static inline int __kvm_irq_line_state(unsigned long *irq_state,void kvm_inject_nmi (struct kvm_vcpu *vcpu);int kvm_get_nr_pending_nmis (struct kvm_vcpu *vcpu); -void kvm_update_dr7 (struct kvm_vcpu *vcpu); -bool __kvm_mmu_unprotect_gfn_and_retry (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,bool always_retry);
@@ -2521,9 +2505,6 @@ u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc);u64 kvm_calc_nested_tsc_offset (u64 l1_offset,u64 l2_offset,u64 l2_multiplier);u64 kvm_calc_nested_tsc_multiplier (u64 l1_multiplier,u64 l2_multiplier); -unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu); -bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip); -void kvm_make_scan_ioapic_request (struct kvm *kvm);void kvm_make_scan_ioapic_request_mask (struct kvm *kvm,unsigned long *vcpu_bitmap);
arch/x86/kvm/regs.h +18 -0
@@ -16,6 +16,18 @@static_assert (!(KVM_POSSIBLE_CR0_GUEST_BITS &X86_CR0_PDPTR_BITS )); +void kvm_post_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0); +void kvm_post_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4); +int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0); +int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3); +int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); +int kvm_set_cr8 (struct kvm_vcpu *vcpu,unsigned long cr8); +int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val); +unsigned long kvm_get_dr (struct kvm_vcpu *vcpu,int dr); +unsigned long kvm_get_cr8 (struct kvm_vcpu *vcpu); +void kvm_lmsw (struct kvm_vcpu *vcpu,unsigned long msw); +int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3); +static inline bool is_long_mode (struct kvm_vcpu *vcpu) {#ifdef CONFIG_X86_64
@@ -425,7 +437,12 @@ static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)return kvm_x86_call (get_segment_base)(vcpu, seg); } +unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu); +bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip); + +unsigned long kvm_get_rflags (struct kvm_vcpu *vcpu);void __kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags); +void kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);void kvm_x86_vcpu_ioctl_get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2);
@@ -436,6 +453,7 @@ void kvm_run_sync_regs_to_user(struct kvm_vcpu *vcpu);int kvm_run_sync_regs_from_user (struct kvm_vcpu *vcpu);void kvm_update_dr0123 (struct kvm_vcpu *vcpu); +void kvm_update_dr7 (struct kvm_vcpu *vcpu);int kvm_vcpu_ioctl_x86_get_debugregs (struct kvm_vcpu *vcpu,struct kvm_debugregs *dbgregs); int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 15/30] KVM: x86: Move register helper declarations from kvm_host.h => regs.h
Quoted 7 lines · Sean Christopherson
Relocate declarations of Control/Debug Register, EFLAGS and RIP helpers from x86's
kvm_host.htoregs.h, to continue trimming downkvm_host.h.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 16/30 KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h
In reply to Sean ChristophersonDefine kvm_{g,s}et_segment() as inline functions in regs.h, as they are literally one-line wrappers to invoke vendor code.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/regs.h | 12 ++++++++++++ arch/x86/kvm/x86.c | 12 ------------ 3 files changed, 12 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
@@ -2303,8 +2303,6 @@ int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu);int kvm_emulate_ap_reset_hold (struct kvm_vcpu *vcpu);int kvm_emulate_wbinvd (struct kvm_vcpu *vcpu); -void kvm_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg); -void kvm_set_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);void kvm_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector); int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
arch/x86/kvm/regs.h +12 -0
@@ -437,6 +437,18 @@ static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)return kvm_x86_call (get_segment_base)(vcpu, seg); } +static inline void kvm_set_segment (struct kvm_vcpu *vcpu, +struct kvm_segment *var,int seg) +{ +kvm_x86_call (set_segment)(vcpu, var, seg); +} + +static inline void kvm_get_segment (struct kvm_vcpu *vcpu, +struct kvm_segment *var,int seg) +{ +kvm_x86_call (get_segment)(vcpu, var, seg); +} +unsigned long kvm_get_linear_rip (struct kvm_vcpu *vcpu);bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip);
arch/x86/kvm/x86.c +0 -12
@@ -4720,18 +4720,6 @@ static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)return handled; } -void kvm_set_segment (struct kvm_vcpu *vcpu, -struct kvm_segment *var,int seg) -{ -kvm_x86_call (set_segment)(vcpu, var, seg); -} - -void kvm_get_segment (struct kvm_vcpu *vcpu, -struct kvm_segment *var,int seg) -{ -kvm_x86_call (get_segment)(vcpu, var, seg); -} -gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) {
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 16/30] KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h
Quoted 8 lines · Sean Christopherson
Define kvm_{g,s}et_segment() as inline functions in
regs.h, as they are literally one-line wrappers to invoke vendor code.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 17/30 KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h
In reply to Sean ChristophersonRelocate declarations of MSR helpers (and kvm_nr_uret_msrs) from x86's kvm_host.h to msrs, to continue trimming down kvm_host.h.
Deliberately leave the funky read_msr() where it is, as it will hopefully be removed entirely as part of a broader kernel-API cleanup.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 26 ------------------------- arch/x86/kvm/msrs.h | 34 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 29 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -26
@@ -2094,7 +2094,6 @@ struct kvm_arch_async_pf {u64 error_code; }; -extern u32 __read_mostly kvm_nr_uret_msrs;extern bool __read_mostly allow_smaller_maxphyaddr;extern bool __read_mostly enable_apicv;extern bool __read_mostly enable_ipiv;
@@ -2278,18 +2277,6 @@ void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu);void kvm_prepare_event_vectoring_exit (struct kvm_vcpu *vcpu,gpa_t gpa);void kvm_prepare_unexpected_reason_exit (struct kvm_vcpu *vcpu,u64 exit_reason); -void kvm_enable_efer_bits (u64 ); -bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer); -int kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); -int kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); -int __kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); -int __kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); -int kvm_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); -int kvm_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); -int kvm_emulate_rdmsr (struct kvm_vcpu *vcpu); -int kvm_emulate_rdmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg); -int kvm_emulate_wrmsr (struct kvm_vcpu *vcpu); -int kvm_emulate_wrmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg);int kvm_emulate_as_nop (struct kvm_vcpu *vcpu);int kvm_emulate_invd (struct kvm_vcpu *vcpu);int kvm_emulate_mwait (struct kvm_vcpu *vcpu);
@@ -2311,9 +2298,6 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr);int kvm_emulate_xsetbv (struct kvm_vcpu *vcpu); -int kvm_get_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); -int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); -int kvm_emulate_rdpmc (struct kvm_vcpu *vcpu);void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr);
@@ -2488,16 +2472,6 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, unsigned long ipi_bitmap_high, u32 min, unsigned long icr, int op_64_bit); -int kvm_add_user_return_msr (u32 msr); -int kvm_find_user_return_msr (u32 msr); -int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); -u64 kvm_get_user_return_msr (unsigned int slot); - -static inline bool kvm_is_supported_user_return_msr (u32 msr) -{ -return kvm_find_user_return_msr (msr) >=0 ; -} -u64 kvm_scale_tsc (u64 tsc,u64 ratio);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc);u64 kvm_calc_nested_tsc_offset (u64 l1_offset,u64 l2_offset,u64 l2_multiplier);
arch/x86/kvm/msrs.h +31 -3
@@ -11,6 +11,8 @@extern bool report_ignored_msrs;extern bool ignore_msrs; +extern u32 __read_mostly kvm_nr_uret_msrs; +static inline void kvm_pr_unimpl_wrmsr (struct kvm_vcpu *vcpu,u32 msr,u64 data) {if (report_ignored_msrs)
@@ -56,13 +58,39 @@ int kvm_get_set_one_reg(struct kvm_vcpu *vcpu, unsigned int ioctl,int kvm_get_reg_list (struct kvm_vcpu *vcpu,struct kvm_reg_list __user *user_list); +void kvm_enable_efer_bits (u64 ); +bool kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer); +int kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); +int kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); +int __kvm_emulate_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); +int __kvm_emulate_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); +int kvm_msr_read (struct kvm_vcpu *vcpu,u32 index,u64 *data); +int kvm_msr_write (struct kvm_vcpu *vcpu,u32 index,u64 data); +int kvm_emulate_rdmsr (struct kvm_vcpu *vcpu); +int kvm_emulate_rdmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg); +int kvm_emulate_wrmsr (struct kvm_vcpu *vcpu); +int kvm_emulate_wrmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg); + +fastpath_t handle_fastpath_wrmsr (struct kvm_vcpu *vcpu); +fastpath_t handle_fastpath_wrmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg); + +int kvm_get_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); +int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); + +int kvm_add_user_return_msr (u32 msr); +int kvm_find_user_return_msr (u32 msr); +int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +u64 kvm_get_user_return_msr (unsigned int slot); + +static inline bool kvm_is_supported_user_return_msr (u32 msr) +{ +return kvm_find_user_return_msr (msr) >=0 ; +} +void kvm_user_return_msr_cpu_online (void );void drop_user_return_notifiers (void );void kvm_destroy_user_return_msrs (void ); -fastpath_t handle_fastpath_wrmsr (struct kvm_vcpu *vcpu); -fastpath_t handle_fastpath_wrmsr_imm (struct kvm_vcpu *vcpu,u32 msr,int reg); -int kvm_emulator_get_msr_with_filter (struct kvm_vcpu *vcpu,u32 msr_index,u64 *pdata); int kvm_emulator_set_msr_with_filter(struct kvm_vcpu *vcpu, u32 msr_index,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 17/30] KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h
Quoted 10 lines · Sean Christopherson
Relocate declarations of MSR helpers (and
kvm_nr_uret_msrs) from x86'skvm_host.hto msrs, to continue trimming downkvm_host.h.Deliberately leave the funky
read_msr()where it is, as it will hopefully be removed entirely as part of a broader kernel-API cleanup.No functional change intended.
Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 18/30 KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
In reply to Sean ChristophersonMove the definition of "struct kvm_x86_msr_filter" and its associate, "struct msr_bitmap_range", to msrs.c, as the details of the filters are very much implementation details that can and should be buried in msrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.
Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 15 ++------------- arch/x86/kvm/msrs.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -13
@@ -315,6 +315,8 @@ enum x86_intercept_stage;struct kvm_kernel_irqfd ;struct kvm_kernel_irq_routing_entry ; +struct kvm_x86_msr_filter ; +struct kvm_caps {/* control of guest tsc rate supported? */ bool has_tsc_control;
@@ -1291,13 +1293,6 @@ struct kvm_hv { };#endif -struct msr_bitmap_range { -u32 flags; -u32 nmsrs; -u32 base; -unsigned long *bitmap; -}; -#ifdef CONFIG_KVM_XEN /* Xen emulation context */ struct kvm_xen {
@@ -1328,12 +1323,6 @@ enum kvm_suppress_eoi_broadcast_mode {KVM_SUPPRESS_EOI_BROADCAST_DISABLED /* Disable Suppress EOI broadcast */ }; -struct kvm_x86_msr_filter { -u8 count; -bool default_allow:1 ; -struct msr_bitmap_range ranges[16 ]; -}; -struct kvm_x86_pmu_event_filter {__u32 action;__u32 nevents;
arch/x86/kvm/msrs.c +13 -0
@@ -32,6 +32,19 @@ static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);#define MAX_IO_MSRS 256 +struct msr_bitmap_range { +u32 flags; +u32 nmsrs; +u32 base; +unsigned long *bitmap; +}; + +struct kvm_x86_msr_filter { +u8 count; +bool default_allow:1 ; +struct msr_bitmap_range ranges[16 ]; +}; +/* * Restoring the host value for MSRs that are only consumed when running in * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 9 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_msr_filter" and its associate, "structmsr_bitmap_range", tomsrs.c, as the details of the filters are very much implementation details that can and should be buried inmsrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.
Quoted 7 lines
Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
Suggested-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
In reply to Sean ChristophersonQuoted 9 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_msr_filter" and its associate, "structmsr_bitmap_range", tomsrs.c, as the details of the filters are very much implementation details that can and should be buried inmsrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.
Quoted 7 lines
Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
Suggested-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
In reply to Sean ChristophersonQuoted 26 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_msr_filter" and its associate, "structmsr_bitmap_range", tomsrs.c, as the details of the filters are very much implementation details that can and should be buried inmsrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
Suggested-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 15 ++------------- arch/x86/kvm/msrs.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h enum x86_intercept_stage; · line 318
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -315,6 +315,8 @@ enum x86_intercept_stage;struct kvm_kernel_irqfd ;struct kvm_kernel_irq_routing_entry ; +struct kvm_x86_msr_filter ;
Just FYI, the code failed to build on my desktop with an old gcc version v9.4. Due to dereferencing pointer to incomplete type ‘struct kvm_x86_msr_filter' via typeof().
It's not the fault of the patch, the issue was fixed for GCC 10+ according to the link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44257
But the minimal tool version got for gcc is still v8.1.0, not sure if it needs to bump the version in scripts/min-tool-version.sh?
--- Build error information below ---
In file included from ./include/linux/rculist.h:11,
from ./include/linux/cgroup.h:15,
from ./include/linux/memcontrol.h:13,
from ./include/linux/resume_user_mode.h:8,
from ./include/linux/entry-virt.h:6,
from ./include/linux/kvm_host.h:5,
from arch/x86/kvm/x86.c:20:arch/x86/kvm/x86.c: In function ‘kvm_arch_destroy_vm’:./include/linux/rcupdate.h:502:9: error: dereferencing pointer to incomplete type ‘struct kvm_x86_msr_filter’
502 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \./include/linux/srcu.h:240:20: note: in expansion of macro ‘__rcu_dereference_check’
240 | __auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/kvm/x86.c:9969:22: note: in expansion of macro ‘srcu_dereference_check’
9969 | kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
| ^~~~~~~~~~~~~~~~~~~~~~
In file included from ./include/linux/notifier.h:16,
from ./arch/x86/include/asm/uprobes.h:13,
from ./include/linux/uprobes.h:66,
from ./include/linux/mm_types.h:16,
from ./include/linux/mmzone.h:22,
from ./include/linux/gfp.h:7,
from ./include/linux/xarray.h:16,
from ./include/linux/list_lru.h:14,
from ./include/linux/fs/super_types.h:7,
from ./include/linux/fs/super.h:5,
from ./include/linux/fs.h:5,
from ./include/linux/cgroup.h:17,
from ./include/linux/memcontrol.h:13,
from ./include/linux/resume_user_mode.h:8,
from ./include/linux/entry-virt.h:6,
from ./include/linux/kvm_host.h:5,
from arch/x86/kvm/x86.c:20:
./include/linux/srcu.h:243:2: error: ‘__v’ undeclared (first use in this function)
243 | __v; \
| ^~~
arch/x86/kvm/x86.c:9969:22: note: in expansion of macro ‘srcu_dereference_check’
9969 | kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
| ^~~~~~~~~~~~~~~~~~~~~~
./include/linux/srcu.h:243:2: note: each undeclared identifier is reported only once for each function it appears in
243 | __v; \
| ^~~
arch/x86/kvm/x86.c:9969:22: note: in expansion of macro ‘srcu_dereference_check’
9969 | kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1));
| ^~~~~~~~~~~~~~~~~~~~~~
In file included from ./include/linux/rculist.h:11,
from ./include/linux/cgroup.h:15,
from ./include/linux/memcontrol.h:13,
from ./include/linux/resume_user_mode.h:8,
from ./include/linux/entry-virt.h:6,
from ./include/linux/kvm_host.h:5,
from arch/x86/kvm/x86.c:20:
./include/linux/rcupdate.h:502:9: error: dereferencing pointer to incomplete type ‘struct kvm_x86_pmu_event_filter’
502 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
./include/linux/srcu.h:240:20: note: in expansion of macro ‘__rcu_dereference_check’
240 | __auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/kvm/x86.c:9975:8: note: in expansion of macro ‘srcu_dereference_check’
9975 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
| ^~~~~~~~~~~~~~~~~~~~~~make[4]: *** [scripts/Makefile.build:289: arch/x86/kvm/x86.o] Error 1 make[3]: *** [scripts/Makefile.build:548: arch/x86/kvm] Error 2 make[2]: *** [scripts/Makefile.build:548: arch/x86] Error 2 make[1]: *** [/home/litbin/workspace/kvm-x86/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 37 lines · Binbin Wu
Quoted 26 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_msr_filter" and its associate, "structmsr_bitmap_range", tomsrs.c, as the details of the filters are very much implementation details that can and should be buried inmsrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
Suggested-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 15 ++------------- arch/x86/kvm/msrs.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0arch/x86/include/asm/kvm_host.h enum x86_intercept_stage; · line 318
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -315,6 +315,8 @@ enum x86_intercept_stage;struct kvm_kernel_irqfd ;struct kvm_kernel_irq_routing_entry ; +struct kvm_x86_msr_filter ;Just FYI, the code failed to build on my desktop with an old gcc version v9.4. Due to dereferencing pointer to incomplete type ‘struct
kvm_x86_msr_filter' viatypeof().
Quoted 2 lines
It's not the fault of the patch, the issue was fixed for GCC 10+ according to the link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44257
This issue was reported at:
rusty 2010-05-24 00:29:46 UTC
But only got fixed at:
Drea Pinski 2021-09-20 02:14:38 UTC
Amazing :-)
Quoted 4 lines
But the minimal tool version got for gcc is still v8.1.0, not sure if it needs to bump the version in
scripts/min-tool-version.sh?
Bumping the version would have global impact, so I don't think we should do.
However, does moving the structure definition to "msrs.h" fix the problem?
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 52 lines · Kai
Quoted 37 lines · Binbin Wu
Quoted 26 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_msr_filter" and its associate, "structmsr_bitmap_range", tomsrs.c, as the details of the filters are very much implementation details that can and should be buried inmsrs.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the MSR code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.Note, this creates a discrepancy with the PMU event filter structure; that will be remedied shortly.
No functional change intended.
Suggested-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 15 ++------------- arch/x86/kvm/msrs.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0arch/x86/include/asm/kvm_host.h enum x86_intercept_stage; · line 318
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -315,6 +315,8 @@ enum x86_intercept_stage;struct kvm_kernel_irqfd ;struct kvm_kernel_irq_routing_entry ; +struct kvm_x86_msr_filter ;Just FYI, the code failed to build on my desktop with an old gcc version v9.4. Due to dereferencing pointer to incomplete type ‘struct
kvm_x86_msr_filter' viatypeof().Quoted 2 lines
It's not the fault of the patch, the issue was fixed for GCC 10+ according to the link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44257
This issue was reported at:
rusty 2010-05-24 00:29:46 UTCBut only got fixed at:
Drea Pinski 2021-09-20 02:14:38 UTCAmazing :-)
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
But the minimal tool version got for gcc is still v8.1.0, not sure if it needs to bump the version in
scripts/min-tool-version.sh?Bumping the version would have global impact, so I don't think we should do.
However, does moving the structure definition to "
msrs.h" fix the problem?
Yes, it does.
Similar issue for struct kvm_x86_pmu_event_filter also could be resolved by moving the structure definition to "pmu.h"
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 6 lines · Binbin Wu
Quoted 1 line
However, does moving the structure definition to "
msrs.h" fix the problem?Yes, it does.
Similar issue for struct
kvm_x86_pmu_event_filteralso could be resolved by moving the structure definition to "pmu.h"
Thanks for confirming. I think we should do this.
Sean Christopherson
maintainerRe: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 9 lines · Kai Huang
Quoted 6 lines · Binbin Wu
Quoted 1 line
However, does moving the structure definition to "
msrs.h" fix the problem?Yes, it does.
Similar issue for struct
kvm_x86_pmu_event_filteralso could be resolved by moving the structure definition to "pmu.h"Thanks for confirming. I think we should do this.
I really don't want to go that route, especially since there's absolutely no reason to use srcu_dereference_check() during destruction. KVM isn't actually checking anything, and the (S)RCU pointers _must_ be protected during destruction, otherwise use-after-free is all but guaranteed.
Unfortunately, every RCU macro I can find does typeof(*p) somewhere in its flow. But I would still strongly prefer to open code a __force to strip the __rcu than expose the structures outside of msrs.c and pmu.c.
This makes gcc8 and gcc9 happy on my end. I'll squash the changes into their respective patches, and update the changelogs.
arch/x86/kvm/x86.c +2 -2
@@ -9975,13 +9975,13 @@ void kvm_arch_destroy_vm(struct kvm *kvm)if (kvm->arch.created_mediated_pmu)perf_release_mediated_pmu ();kvm_destroy_vcpus (kvm); -kvm_free_msr_filter (srcu_dereference_check (kvm->arch.msr_filter, &kvm->srcu,1 )); +kvm_free_msr_filter ((void *__force )kvm->arch.msr_filter);#ifdef CONFIG_KVM_IOAPIC kvm_pic_destroy (kvm);kvm_ioapic_destroy (kvm);#endif kvfree (rcu_dereference_check (kvm->arch.apic_map,1 )); -kfree (srcu_dereference_check (kvm->arch.pmu_event_filter, &kvm->srcu,1 )); +kfree ((void *__force )kvm->arch.pmu_event_filter);kvm_mmu_uninit_vm (kvm);kvm_page_track_cleanup (kvm);kvm_xen_destroy_vm (kvm);
Re: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Kai Huang
Quoted 6 lines · Binbin Wu
Quoted 1 line
However, does moving the structure definition to "
msrs.h" fix the problem?Yes, it does.
Similar issue for struct
kvm_x86_pmu_event_filteralso could be resolved by moving the structure definition to "pmu.h"
Quoted 6 lines
Quoted 1 line
Thanks for confirming. I think we should do this.
I really don't want to go that route, especially since there's absolutely no reason to use
srcu_dereference_check()during destruction. KVM isn't actually checking anything, and the (S)RCU pointers _must_ be protected during destruction, otherwise use-after-free is all but guaranteed.
Yeah agreed.
Quoted 7 lines
Unfortunately, every RCU macro I can find does
typeof(*p)somewhere in its flow. But I would still strongly prefer to open code a__forceto strip the__rcuthan expose the structures outside ofmsrs.candpmu.c.This makes gcc8 and gcc9 happy on my end. I'll squash the changes into their respective patches, and update the changelogs.
Fine to me (seems we are having more and more functional changes in this series but this particular change may not be the case).
FWIW, I also tested CLANG that your diff builds successfully. I only tried clang version 20.1.8 (Fedora 20.1.8-4.fc42) since I don't have other versions.
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c void kvm_arch_destroy_vm(struct kvm *kvm) · line 9983
Quoted 10 lines
arch/x86/kvm/x86.c +1
-1@@ -9975,13 +9975,13 @@ void kvm_arch_destroy_vm(struct kvm *kvm)if (kvm->arch.created_mediated_pmu)perf_release_mediated_pmu ();kvm_destroy_vcpus (kvm); -kvm_free_msr_filter (srcu_dereference_check (kvm->arch.msr_filter, &kvm->srcu,1 )); +kvm_free_msr_filter ((void *__force )kvm->arch.msr_filter);#ifdef CONFIG_KVM_IOAPIC kvm_pic_destroy (kvm);kvm_ioapic_destroy (kvm);#endif kvfree (rcu_dereference_check (kvm->arch.apic_map,1 ));
Btw, this made me look at apic_map too. And seems we can also move "struct kvm_apic_map" to "lapic.h". We cannot move it to lapic.c because kvm_sched_yield() in x86.c actually references the members of the structure.
Sean Christopherson
maintainerRe: [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c
Quoted 11 lines · Kai Huang
Quoted 7 lines · Sean Christopherson
On Tue, Jun 16, 2026, Kai Huang wrote:
Unfortunately, every RCU macro I can find doestypeof(*p)somewhere in its flow. But I would still strongly prefer to open code a__forceto strip the__rcuthan expose the structures outside ofmsrs.candpmu.c.This makes gcc8 and gcc9 happy on my end. I'll squash the changes into their respective patches, and update the changelogs.
Fine to me (seems we are having more and more functional changes in this series but this particular change may not be the case).
Yeah, because KVM hardcodes a '1' to the dereference check, in practice this isn't a functional change.
Quoted 5 lines
Quoted 1 line
kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));Btw, this made me look at
apic_maptoo. And seems we can also move "structkvm_apic_map" to "lapic.h". We cannot move it tolapic.cbecausekvm_sched_yield()inx86.cactually references the members of the structure.
Ya, good eyes, I'll slip in yet another patch.
Sean Christopherson
maintainerPATCH v4 19/30 KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to pmu.c
In reply to Sean ChristophersonMove the definition of "struct kvm_x86_pmu_event_filter" to pmu.c, as the as the details of the filters are very much implementation details that can and should be buried in pmu.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the PMU code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.
No functional change intended
--- arch/x86/include/asm/kvm_host.h | 13 +------------ arch/x86/kvm/pmu.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -12
@@ -316,6 +316,7 @@ struct kvm_kernel_irqfd;struct kvm_kernel_irq_routing_entry ;struct kvm_x86_msr_filter ; +struct kvm_x86_pmu_event_filter ;struct kvm_caps {/* control of guest tsc rate supported? */
@@ -1323,18 +1324,6 @@ enum kvm_suppress_eoi_broadcast_mode {KVM_SUPPRESS_EOI_BROADCAST_DISABLED /* Disable Suppress EOI broadcast */ }; -struct kvm_x86_pmu_event_filter { -__u32 action; -__u32 nevents; -__u32 fixed_counter_bitmap; -__u32 flags; -__u32 nr_includes; -__u32 nr_excludes; -__u64 *includes; -__u64 *excludes; - __u64 events[] __counted_by(nevents); -}; -enum kvm_apicv_inhibit {/********************************************************************/
arch/x86/kvm/pmu.c +12 -0
@@ -43,6 +43,18 @@ module_param(enable_pmu, bool, 0444);bool __read_mostly enable_mediated_pmu;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_mediated_pmu); +struct kvm_x86_pmu_event_filter { +__u32 action; +__u32 nevents; +__u32 fixed_counter_bitmap; +__u32 flags; +__u32 nr_includes; +__u32 nr_excludes; +__u64 *includes; +__u64 *excludes; + __u64 events[] __counted_by(nevents); +}; +struct kvm_pmu_emulated_event_selectors {u64 INSTRUCTIONS_RETIRED;u64 BRANCH_INSTRUCTIONS_RETIRED;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 19/30] KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to pmu.c
Quoted 11 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_pmu_event_filter" topmu.c, as the as the details of the filters are very much implementation details that can and should be buried inpmu.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the PMU code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.No functional change intended
Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 19/30] KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to pmu.c
In reply to Sean ChristophersonQuoted 2 lines · Sean Christopherson
Move the definition of "struct
kvm_x86_pmu_event_filter" topmu.c, as the as the details of the filters are very much implementation details that can
^
duplicated "as the"
Quoted 9 lines
and should be buried in
pmu.c. While the _existence_ of filters is public knowledge, almost by definition, the contents don't need to be exposed outside of the PMU code as the filter data is provided by userspace, i.e. it pretty much has to be dynamically allocated, and thus never should be fully embedded in a globally visible structure.No functional change intended
Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 20/30 KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h
In reply to Sean ChristophersonMove a pile of MMU helper declarations and macros into mmu.h, as they are very much KVM x86 internal APIs and details, and not intended to be exposed to arch-neutral KVM, and certainly not to the broader kernel.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 70 -------------------------------- arch/x86/kvm/mmu.h | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 70 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -70
@@ -161,12 +161,6 @@#define KVM_HPAGE_MASK (x) (~(KVM_HPAGE_SIZE(x) - 1))#define KVM_PAGES_PER_HPAGE (x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE) -#define KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO 50 -#define KVM_MIN_ALLOC_MMU_PAGES 64UL -#define KVM_MMU_HASH_SHIFT 12 -#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT) -#define KVM_MIN_FREE_MMU_PAGES 5 -#define KVM_REFILL_PAGES 25#define KVM_MAX_CPUID_ENTRIES 256#define KVM_NR_VAR_MTRR 8
@@ -2131,38 +2125,6 @@ enum kvm_intr_type { ((vcpu) && (vcpu)->arch.handling_intr_from_guest && \ (!!in_nmi () == ((vcpu)->arch.handling_intr_from_guest ==KVM_HANDLING_NMI ))) -void __init kvm_mmu_x86_module_init (void ); -int kvm_mmu_vendor_module_init (void ); -void kvm_mmu_vendor_module_exit (void ); - -void kvm_mmu_destroy (struct kvm_vcpu *vcpu); -int kvm_mmu_create (struct kvm_vcpu *vcpu); -int kvm_mmu_init_vm (struct kvm *kvm); -void kvm_mmu_uninit_vm (struct kvm *kvm); - -void kvm_mmu_init_memslot_memory_attributes (struct kvm *kvm, -struct kvm_memory_slot *slot); - -void kvm_mmu_after_set_cpuid (struct kvm_vcpu *vcpu); -void kvm_mmu_reset_context (struct kvm_vcpu *vcpu); -void kvm_mmu_slot_remove_write_access (struct kvm *kvm, -const struct kvm_memory_slot *memslot, -int start_level); -void kvm_mmu_slot_try_split_huge_pages (struct kvm *kvm, -const struct kvm_memory_slot *memslot, -int target_level); -void kvm_mmu_try_split_huge_pages (struct kvm *kvm, -const struct kvm_memory_slot *memslot, -u64 start,u64 end, -int target_level); -void kvm_mmu_recover_huge_pages (struct kvm *kvm, -const struct kvm_memory_slot *memslot); -void kvm_mmu_slot_leaf_clear_dirty (struct kvm *kvm, -const struct kvm_memory_slot *memslot); -void kvm_mmu_invalidate_mmio_sptes (struct kvm *kvm,u64 gen); -void kvm_mmu_change_mmu_pages (struct kvm *kvm,unsigned long kvm_nr_mmu_pages); -void kvm_zap_gfn_range (struct kvm *kvm,gfn_t gfn_start,gfn_t gfn_end); -/* * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing * userspace I/O) to indicate that the emulation context
@@ -2312,25 +2274,6 @@ static inline int __kvm_irq_line_state(unsigned long *irq_state,void kvm_inject_nmi (struct kvm_vcpu *vcpu);int kvm_get_nr_pending_nmis (struct kvm_vcpu *vcpu); -bool __kvm_mmu_unprotect_gfn_and_retry (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, -bool always_retry); - -static inline bool kvm_mmu_unprotect_gfn_and_retry (struct kvm_vcpu *vcpu, -gpa_t cr2_or_gpa) -{ -return __kvm_mmu_unprotect_gfn_and_retry (vcpu, cr2_or_gpa, false); -} - -void kvm_mmu_free_roots (struct kvm *kvm,struct kvm_mmu *mmu, -ulong roots_to_free); -void kvm_mmu_free_guest_mode_roots (struct kvm *kvm,struct kvm_mmu *mmu); -gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception); -gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception); -gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception); -bool kvm_apicv_activated (struct kvm *kvm);bool kvm_vcpu_apicv_activated (struct kvm_vcpu *vcpu);void __kvm_vcpu_update_apicv (struct kvm_vcpu *vcpu);
@@ -2363,19 +2306,6 @@ static inline void kvm_dec_apicv_irq_window_req(struct kvm *kvm)kvm_inc_or_dec_irq_window_inhibit (kvm, false); } -int kvm_mmu_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u64 error_code, -void *insn,int insn_len); -void kvm_mmu_print_sptes (struct kvm_vcpu *vcpu,gpa_t gpa,const char *msg); -void kvm_mmu_invlpg (struct kvm_vcpu *vcpu,gva_t gva); -void kvm_mmu_invalidate_addr (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -u64 addr,unsigned long roots); -void kvm_mmu_invpcid_gva (struct kvm_vcpu *vcpu,gva_t gva,unsigned long pcid); -void kvm_mmu_new_pgd (struct kvm_vcpu *vcpu,gpa_t new_pgd); - -void kvm_configure_mmu (bool enable_tdp,int tdp_forced_root_level, -int tdp_max_root_level,int tdp_huge_page_level); - -#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES #define kvm_arch_has_private_mem (kvm) ((kvm)->arch.has_private_mem)#endif
arch/x86/kvm/mmu.h +71 -0
95 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 20/30] KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h
Quoted 9 lines · Sean Christopherson
Move a pile of MMU helper declarations and macros into
mmu.h, as they are very much KVM x86 internal APIs and details, and not intended to be exposed to arch-neutral KVM, and certainly not to the broader kernel.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 21/30 KVM: x86: Move LLDT assembly wrappers into VMX
In reply to Sean ChristophersonMove kvm_{load,read}_ldt() into vmx.c, as vmx_{load,store}_ldt(), as they are exclusively used by VMX to save/restore host state, and have no business being globally visible.
Ideally, KVM-specific helpers wouldn't exist at all, as they are nothing more than assembly wrappers for SLDT and LLDT, i.e. should be provided by the kernel, not by KVM. Punt that cleanup to the future, as arch/x86/include/asm/desc.h _does_ provide helpers, but load_ldt() is only available for CONFIG_PARAVIRT_XXL=n builds, and both {load,store}_ldt() unnecessarily constrain the operands to memory.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 12 ------------ arch/x86/kvm/vmx/vmx.c | 18 +++++++++++++++--- 2 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -12
@@ -2312,18 +2312,6 @@ static inline void kvm_dec_apicv_irq_window_req(struct kvm *kvm)#define kvm_arch_has_readonly_mem (kvm) (!(kvm)->arch.has_protected_state) -static inline u16 kvm_read_ldt (void ) -{ -u16 ldt; - asm("sldt %0" :"=g" (ldt)); -return ldt; -} - -static inline void kvm_load_ldt (u16 sel) -{ - asm("lldt %0" : :"rm" (sel)); -} -#ifdef CONFIG_X86_64 static inline unsigned long read_msr (unsigned long msr) {
arch/x86/kvm/vmx/vmx.c +15 -3
@@ -1186,6 +1186,18 @@ static void vmx_remove_autostore_msr(struct vcpu_vmx *vmx, u32 msr)vmx_remove_auto_msr (&vmx->msr_autostore, msr,VM_EXIT_MSR_STORE_COUNT ); } +static u16 vmx_store_ldt (void ) +{ +u16 ldt; + asm("sldt %0" :"=g" (ldt)); +return ldt; +} + +static void vmx_load_ldt (u16 sel) +{ + asm("lldt %0" : :"rm" (sel)); +} +#ifdef CONFIG_X86_32 /* * On 32-bit kernels, VM exits still load the FS and GS bases from the
@@ -1203,7 +1215,7 @@ static unsigned long segment_base(u16 selector) table =get_current_gdt_ro ();if ((selector &SEGMENT_TI_MASK ) ==SEGMENT_LDT ) { -u16 ldt_selector =kvm_read_ldt (); +u16 ldt_selector =vmx_store_ldt ();if (!(ldt_selector & ~SEGMENT_RPL_MASK ))return 0 ;
@@ -1358,7 +1370,7 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)* Set host fs and gs selectors. Unfortunately, 22.2.3 does not * allow segment selectors with cpl > 0 or ti == 1. */ - host_state->ldt_sel =kvm_read_ldt (); + host_state->ldt_sel =vmx_store_ldt ();#ifdef CONFIG_X86_64 savesegment (ds, host_state->ds_sel);
@@ -1405,7 +1417,7 @@ static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)rdmsrq (MSR_KERNEL_GS_BASE , vmx->msr_guest_kernel_gs_base);#endif if (host_state->ldt_sel || (host_state->gs_sel &7 )) { -kvm_load_ldt (host_state->ldt_sel); +vmx_load_ldt (host_state->ldt_sel);#ifdef CONFIG_X86_64 load_gs_index (host_state->gs_sel);#else
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 21/30] KVM: x86: Move LLDT assembly wrappers into VMX
Quoted 11 lines · Sean Christopherson
Move kvm_{load,read}_ldt() into
vmx.c, as vmx_{load,store}_ldt(), as they are exclusively used by VMX to save/restore host state, and have no business being globally visible.Ideally, KVM-specific helpers wouldn't exist at all, as they are nothing more than assembly wrappers for SLDT and LLDT, i.e. should be provided by the kernel, not by KVM. Punt that cleanup to the future, as
arch/x86/include/asm/desc.h_does_ provide helpers, butload_ldt()is only available forCONFIG_PARAVIRT_XXL=nbuilds, and both {load,store}_ldt() unnecessarily constrain the operands to memory.
Quoted 3 lines
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 22/30 KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c
In reply to Sean ChristophersonMove a variety of "VALID MASK" defines, e.g. that capture which flags in a given ioctl are supported by KVM, from kvm_host.h to x86.c. The set of valid flags/bits is very much a KVM-internal detail, as the values from the hardcoded #defines are often captured and massaged by KVM's setup code, i.e. *directly* using the macros outside of KVM x86 would be actively dangerous.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 34 --------------------------------- arch/x86/kvm/x86.c | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 34 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -34
@@ -78,12 +78,6 @@#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ KVM_DIRTY_LOG_INITIALLY_SET) -#define KVM_BUS_LOCK_DETECTION_VALID_MODE (KVM_BUS_LOCK_DETECTION_OFF | \ - KVM_BUS_LOCK_DETECTION_EXIT) - -#define KVM_X86_NOTIFY_VMEXIT_VALID_BITS (KVM_X86_NOTIFY_VMEXIT_ENABLED | \ - KVM_X86_NOTIFY_VMEXIT_USER) -/* x86-specific vcpu->requests bit members */ #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)#define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
@@ -2417,34 +2411,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)int memslot_rmap_alloc (struct kvm_memory_slot *slot,unsigned long npages); -#define KVM_CLOCK_VALID_FLAGS \ - (KVM_CLOCK_TSC_STABLE | KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC) - -#define KVM_X86_VALID_QUIRKS \ - (KVM_X86_QUIRK_LINT0_REENABLED | \ - KVM_X86_QUIRK_CD_NW_CLEARED | \ - KVM_X86_QUIRK_LAPIC_MMIO_HOLE | \ - KVM_X86_QUIRK_OUT_7E_INC_RIP | \ - KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT | \ - KVM_X86_QUIRK_FIX_HYPERCALL_INSN | \ - KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS | \ - KVM_X86_QUIRK_SLOT_ZAP_ALL | \ - KVM_X86_QUIRK_STUFF_FEATURE_MSRS | \ - KVM_X86_QUIRK_IGNORE_GUEST_PAT | \ - KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM | \ - KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT) - -#define KVM_X86_CONDITIONAL_QUIRKS \ - (KVM_X86_QUIRK_CD_NW_CLEARED | \ - KVM_X86_QUIRK_IGNORE_GUEST_PAT) - -/* -* KVM previously used a u32 field in kvm_run to indicate the hypercall was -* initiated from long mode. KVM now sets bit 0 to indicate long mode, but the -* remaining 31 lower bits must be 0 to preserve ABI. -*/ -#define KVM_EXIT_HYPERCALL_MBZ GENMASK_ULL(31, 1) -static inline bool kvm_arch_has_irq_bypass (void ) {return enable_device_posted_irqs;
arch/x86/kvm/x86.c +33 -0
@@ -105,6 +105,12 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host);#define emul_to_vcpu (ctxt) \ ((struct kvm_vcpu *)(ctxt)->vcpu) +/* +* KVM previously used a u32 field in kvm_run to indicate the hypercall was +* initiated from long mode. KVM now sets bit 0 to indicate long mode, but the +* remaining 31 lower bits must be 0 to preserve ABI. +*/ +#define KVM_EXIT_HYPERCALL_MBZ GENMASK_ULL(31, 1)#define KVM_EXIT_HYPERCALL_VALID_MASK (1 << KVM_HC_MAP_GPA_RANGE)#define KVM_CAP_PMU_VALID_MASK KVM_PMU_CAP_DISABLE
@@ -114,6 +120,33 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host); KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST | \ KVM_X2APIC_DISABLE_SUPPRESS_EOI_BROADCAST) +#define KVM_CLOCK_VALID_FLAGS \ + (KVM_CLOCK_TSC_STABLE | KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC) + +#define KVM_X86_VALID_QUIRKS \ + (KVM_X86_QUIRK_LINT0_REENABLED | \ + KVM_X86_QUIRK_CD_NW_CLEARED | \ + KVM_X86_QUIRK_LAPIC_MMIO_HOLE | \ + KVM_X86_QUIRK_OUT_7E_INC_RIP | \ + KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT | \ + KVM_X86_QUIRK_FIX_HYPERCALL_INSN | \ + KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS | \ + KVM_X86_QUIRK_SLOT_ZAP_ALL | \ + KVM_X86_QUIRK_STUFF_FEATURE_MSRS | \ + KVM_X86_QUIRK_IGNORE_GUEST_PAT | \ + KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM | \ + KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT) + +#define KVM_X86_CONDITIONAL_QUIRKS \ + (KVM_X86_QUIRK_CD_NW_CLEARED | \ + KVM_X86_QUIRK_IGNORE_GUEST_PAT) + +#define KVM_BUS_LOCK_DETECTION_VALID_MODE (KVM_BUS_LOCK_DETECTION_OFF | \ + KVM_BUS_LOCK_DETECTION_EXIT) + +#define KVM_X86_NOTIFY_VMEXIT_VALID_BITS (KVM_X86_NOTIFY_VMEXIT_ENABLED | \ + KVM_X86_NOTIFY_VMEXIT_USER) +static void process_nmi (struct kvm_vcpu *vcpu);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 22/30] KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c
Quoted 11 lines · Sean Christopherson
Move a variety of "VALID MASK" defines, e.g. that capture which flags in a given ioctl are supported by KVM, from
kvm_host.htox86.c. The set of valid flags/bits is very much a KVM-internal detail, as the values from the hardcoded #defines are often captured and massaged by KVM's setup code, i.e. *directly* using the macros outside of KVM x86 would be actively dangerous.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 23/30 KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h
In reply to Sean ChristophersonBury __kvm_irq_line_state() in CONFIG_KVM_IOAPIC=y code, as it's only used by PIC and I/O APIC code.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 12 ------------ arch/x86/kvm/ioapic.h | 12 ++++++++++++ 2 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -12
@@ -2253,18 +2253,6 @@ static inline void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr); -static inline int __kvm_irq_line_state (unsigned long *irq_state, -int irq_source_id,int level) -{ -/* Logical OR for level trig interrupt */ -if (level) -__set_bit (irq_source_id, irq_state); -else -__clear_bit (irq_source_id, irq_state); - -return !!(*irq_state); -} -void kvm_inject_nmi (struct kvm_vcpu *vcpu);int kvm_get_nr_pending_nmis (struct kvm_vcpu *vcpu);
arch/x86/kvm/ioapic.h +12 -0
@@ -113,6 +113,18 @@ void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);void kvm_set_ioapic (struct kvm *kvm,struct kvm_ioapic_state *state);void kvm_ioapic_scan_entry (struct kvm_vcpu *vcpu,ulong *ioapic_handled_vectors); + +static inline int __kvm_irq_line_state (unsigned long *irq_state, +int irq_source_id,int level) +{ +/* Logical OR for level trig interrupt */ +if (level) +__set_bit (irq_source_id, irq_state); +else +__clear_bit (irq_source_id, irq_state); + +return !!(*irq_state); +}#endif /* CONFIG_KVM_IOAPIC */ static inline int ioapic_in_kernel (struct kvm *kvm)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 23/30] KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h
Quoted 8 lines · Sean Christopherson
Bury
__kvm_irq_line_state()inCONFIG_KVM_IOAPIC=ycode, as it's only used by PIC and I/O APIC code.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 24/30 KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h
In reply to Sean ChristophersonMove the function declaration for APIs to get/query pending IRQs from kvm_host.h to irq.h, as the APIs are only used by KVM x86 code.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 5 ----- arch/x86/kvm/irq.h | 6 ++++++ arch/x86/kvm/svm/nested.c | 1 + arch/x86/kvm/vmx/nested.c | 1 + 4 files changed, 8 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -5
@@ -2338,12 +2338,7 @@ enum {# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif -int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v); -int kvm_cpu_has_interrupt (struct kvm_vcpu *vcpu); -int kvm_cpu_has_extint (struct kvm_vcpu *v);int kvm_arch_interrupt_allowed (struct kvm_vcpu *vcpu); -int kvm_cpu_get_extint (struct kvm_vcpu *v); -int kvm_cpu_get_interrupt (struct kvm_vcpu *v);void kvm_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
arch/x86/kvm/irq.h +6 -0
@@ -112,6 +112,12 @@ static inline int irqchip_in_kernel(struct kvm *kvm)return mode !=KVM_IRQCHIP_NONE ; } +int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v); +int kvm_cpu_has_interrupt (struct kvm_vcpu *vcpu); +int kvm_cpu_has_extint (struct kvm_vcpu *v); +int kvm_cpu_get_extint (struct kvm_vcpu *v); +int kvm_cpu_get_interrupt (struct kvm_vcpu *v); +void kvm_inject_pending_timer_irqs (struct kvm_vcpu *vcpu);void kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu);void kvm_apic_nmi_wd_deliver (struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/nested.c +1 -0
@@ -23,6 +23,7 @@#include "kvm_emulate.h" #include "trace.h" +#include "irq.h" #include "mmu.h" #include "x86.h" #include "smm.h"
arch/x86/kvm/vmx/nested.c +1 -0
@@ -11,6 +11,7 @@#include "x86.h" #include "cpuid.h" #include "hyperv.h" +#include "irq.h" #include "mmu.h" #include "nested.h" #include "pmu.h"
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 24/30] KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h
Quoted 8 lines · Sean Christopherson
Move the function declaration for APIs to get/query pending IRQs from
kvm_host.htoirq.h, as the APIs are only used by KVM x86 code.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v4 24/30] KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h
In reply to Sean ChristophersonQuoted 7 lines · Sean Christopherson
Move the function declaration for APIs to get/query pending IRQs from
kvm_host.htoirq.h, as the APIs are only used by KVM x86 code.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 25/30 KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h => lapic.h
In reply to Sean ChristophersonMove the declaration of kvm_pv_send_ipi() into lapic.h, as its implementation is provided by lapic.c (sending PV IPIs relies on the optimized APIC map provided by the in-kernel local APIC), and it's only used by KVM x86 code.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 4 ---- arch/x86/kvm/lapic.h | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -4
@@ -2341,10 +2341,6 @@ enum {int kvm_arch_interrupt_allowed (struct kvm_vcpu *vcpu);void kvm_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); -int kvm_pv_send_ipi (struct kvm *kvm,unsigned long ipi_bitmap_low, -unsigned long ipi_bitmap_high,u32 min, -unsigned long icr,int op_64_bit); -u64 kvm_scale_tsc (u64 tsc,u64 ratio);u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc);u64 kvm_calc_nested_tsc_offset (u64 l1_offset,u64 l2_offset,u64 l2_multiplier);
arch/x86/kvm/lapic.h +3 -0
@@ -131,6 +131,9 @@ static inline int kvm_irq_delivery_to_apic(struct kvm *kvm, }void kvm_apic_send_ipi (struct kvm_lapic *apic,u32 icr_low,u32 icr_high); +int kvm_pv_send_ipi (struct kvm *kvm,unsigned long ipi_bitmap_low, +unsigned long ipi_bitmap_high,u32 min, +unsigned long icr,int op_64_bit);int kvm_apic_set_base (struct kvm_vcpu *vcpu,u64 value,bool host_initiated);int kvm_apic_get_state (struct kvm_vcpu *vcpu,struct kvm_lapic_state *s);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 25/30] KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h => lapic.h
Quoted 10 lines · Sean Christopherson
Move the declaration of
kvm_pv_send_ipi()intolapic.h, as its implementation is provided bylapic.c(sending PV IPIs relies on the optimized APIC map provided by the in-kernel local APIC), and it's only used by KVM x86 code.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 26/30 KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
In reply to Sean ChristophersonWhen querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_ nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.
For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only kvm_vcpu_ready_for_interrupt_injection() is affected in practice. kvm_vcpu_has_events() is unreachable when a nested run is pending, as KVM clears nested_run_pending prior to calling kvm_emulate_halt_noskip() when putting L2 into HLT via GUEST_ACTIVITY_HLT, and SVM has no equivalent to GUEST_ACTIVITY_STATE. I.e. the vCPU will always be runnable if a nested run is pending, and thus kvm_arch_vcpu_runnable() => kvm_vcpu_has_events() is effectively dead code, as is __kvm_emulate_halt() => kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly, kvm_can_do_async_pf() is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.
As for kvm_vcpu_ready_for_interrupt_injection(), incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT. But, KVM will still hold/block the ExtINT and request its own IRQ window. I.e. the net effect is more or less the same as the for-injection case.
--- arch/x86/kvm/svm/svm.c | 6 +++--- arch/x86/kvm/vmx/vmx.c | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +3 -3
@@ -4055,12 +4055,12 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) {struct vcpu_svm *svm =to_svm (vcpu); -if (vcpu->arch.nested_run_pending) -return -EBUSY ; -if (svm_interrupt_blocked (vcpu))return 0 ; +if (vcpu->arch.nested_run_pending) +return -EBUSY ; +/* * An IRQ must not be injected into L2 if it's supposed to VM-Exit, * e.g. if the IRQ arrived asynchronously after checking nested events.
arch/x86/kvm/vmx/vmx.c +4 -1
@@ -5254,6 +5254,9 @@ bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)int vmx_interrupt_allowed (struct kvm_vcpu *vcpu,bool for_injection) { +if (vmx_interrupt_blocked (vcpu)) +return 0 ; +if (vcpu->arch.nested_run_pending)return -EBUSY ;
@@ -5264,7 +5267,7 @@ int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)if (for_injection &&is_guest_mode (vcpu) &&nested_exit_on_intr (vcpu))return -EBUSY ; -return !vmx_interrupt_blocked (vcpu); +return 1 ; }int vmx_set_tss_addr (struct kvm *kvm,unsigned int addr)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 24 lines · Sean Christopherson
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_
nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only
kvm_vcpu_ready_for_interrupt_injection()is affected in practice.kvm_vcpu_has_events()is unreachable when a nested run is pending, as KVM clearsnested_run_pendingprior to callingkvm_emulate_halt_noskip()when putting L2 into HLT viaGUEST_ACTIVITY_HLT, and SVM has no equivalent toGUEST_ACTIVITY_STATE. I.e. the vCPU will always be runnable if a nested run is pending, and thuskvm_arch_vcpu_runnable()=>kvm_vcpu_has_events()is effectively dead code, as is__kvm_emulate_halt()=>kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly,kvm_can_do_async_pf()is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.
Quoted 3 lines
As for
kvm_vcpu_ready_for_interrupt_injection(), incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT.
"incorrectly treating interrupts as being allowed" is the status quo, that this patch fixes, not sth this patch introduces -- right?
The changelog reads like for the non-injection case this change might not be the right thing to do, but I don't think this is the case? I assume returning false from kvm_vcpu_ready_for_interrupt_injection() and kvm_vcpu_has_events() if L1's interrupts are blocked while L2 is running is the right thing to do?
The code makes sense to me but I am trying to make sense of the changelog.
Aside from that, I have two comments about existing issues (Sashiko-style):
1. The return values of {vmx/svm}_interrupt_allowed() are annoying. IIUC, 0 is not allowed, 1 is allowed, and -EBUSY is generally allowed but not right now, request immediate exit and try again? That should be documented somewhere (maybe I just missed it?).
2. Aside from TDX, seems like {vmx/svm}_interrupt_allowed() are doing the same thing? So maybe move all that logic into kvm_arch_interrupt_allowed(), rename it (because it's only used by x86), and make interrupt_blocked() the actual per-vendor callback? I assume this is the case because nested_run_pending used to be per-vendor, but now we can clean this up. For TDX, I assume the per-vendor hook can just be tdx_interrupt_allowed() reversed?
Quoted 56 lines
But, KVM will still hold/block the ExtINT
and request its own IRQ window. I.e. the net effect is more or less the same as the for-injection case.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/svm.c | 6 +++--- arch/x86/kvm/vmx/vmx.c | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-)arch/x86/kvm/svm/svm.c +3
-3@@ -4055,12 +4055,12 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) {struct vcpu_svm *svm =to_svm (vcpu); -if (vcpu->arch.nested_run_pending) -return -EBUSY ; -if (svm_interrupt_blocked (vcpu))return 0 ; +if (vcpu->arch.nested_run_pending) +return -EBUSY ; +/* * An IRQ must not be injected into L2 if it's supposed to VM-Exit, * e.g. if the IRQ arrived asynchronously after checking nested events. arch/x86/kvm/vmx/vmx.c +4
-2@@ -5254,6 +5254,9 @@ bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)int vmx_interrupt_allowed (struct kvm_vcpu *vcpu,bool for_injection) { +if (vmx_interrupt_blocked (vcpu)) +return 0 ; +if (vcpu->arch.nested_run_pending)return -EBUSY ;@@ -5264,7 +5267,7 @@ int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)if (for_injection &&is_guest_mode (vcpu) &&nested_exit_on_intr (vcpu))return -EBUSY ; -return !vmx_interrupt_blocked (vcpu); +return 1 ; }int vmx_set_tss_addr (struct kvm *kvm,unsigned int addr) --2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 40 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_
nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only
kvm_vcpu_ready_for_interrupt_injection()is affected in practice.kvm_vcpu_has_events()is unreachable when a nested run is pending, as KVM clearsnested_run_pendingprior to callingkvm_emulate_halt_noskip()when putting L2 into HLT viaGUEST_ACTIVITY_HLT, and SVM has no equivalent toGUEST_ACTIVITY_STATE. I.e. the vCPU will always be runnable if a nested run is pending, and thuskvm_arch_vcpu_runnable()=>kvm_vcpu_has_events()is effectively dead code, as is__kvm_emulate_halt()=>kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly,kvm_can_do_async_pf()is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.Quoted 3 lines
As for
kvm_vcpu_ready_for_interrupt_injection(), incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT."incorrectly treating interrupts as being allowed" is the status quo, that this patch fixes, not sth this patch introduces -- right?
The changelog reads like for the non-injection case this change might not be the right thing to do, but I don't think this is the case? I assume returning false from
kvm_vcpu_ready_for_interrupt_injection()andkvm_vcpu_has_events()if L1's interrupts are blocked while L2 is running is the right thing to do?
Quoted 8 lines
The code makes sense to me but I am trying to make sense of the changelog.
Aside from that, I have two comments about existing issues (Sashiko-style):
1. The return values of {vmx/svm}_interrupt_allowed() are annoying. IIUC, 0 is not allowed, 1 is allowed, and
-EBUSYis generally allowed but not right now, request immediate exit and try again? That should be documented somewhere (maybe I just missed it?).
The next patch is adding the documentation, I spoke too soon :)
Quoted 8 lines
2. Aside from TDX, seems like {vmx/svm}_interrupt_allowed() are doing the same thing? So maybe move all that logic into
kvm_arch_interrupt_allowed(), rename it (because it's only used by x86), and makeinterrupt_blocked()the actual per-vendor callback? I assume this is the case becausenested_run_pendingused to be per-vendor, but now we can clean this up. For TDX, I assume the per-vendor hook can just betdx_interrupt_allowed()reversed?
It also does the renaming (nice!), so we're halfway there. I think for this series, we should at least convert all direct calls to the vendor .interrupt_allowed through the new kvm_is_interrupt_allowed() helper. Unless you wanna go all the way and rework .interrupt_allowed to .interrupt_blocked while at it as well.
Sean Christopherson
maintainerRe: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 43 lines · Yosry Ahmed
Quoted 42 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_
nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only
kvm_vcpu_ready_for_interrupt_injection()is affected in practice.kvm_vcpu_has_events()is unreachable when a nested run is pending, as KVM clearsnested_run_pendingprior to callingkvm_emulate_halt_noskip()when putting L2 into HLT viaGUEST_ACTIVITY_HLT, and SVM has no equivalent toGUEST_ACTIVITY_STATE. I.e. the vCPU will always be runnable if a nested run is pending, and thuskvm_arch_vcpu_runnable()=>kvm_vcpu_has_events()is effectively dead code, as is__kvm_emulate_halt()=>kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly,kvm_can_do_async_pf()is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.Quoted 3 lines
As for
kvm_vcpu_ready_for_interrupt_injection(), incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT."incorrectly treating interrupts as being allowed" is the status quo, that this patch fixes, not sth this patch introduces -- right?
The changelog reads like for the non-injection case this change might not be the right thing to do, but I don't think this is the case? I assume returning false from
kvm_vcpu_ready_for_interrupt_injection()andkvm_vcpu_has_events()if L1's interrupts are blocked while L2 is running is the right thing to do?The code makes sense to me but I am trying to make sense of the changelog.
Quoted 14 lines
Quoted 6 lines
Aside from that, I have two comments about existing issues (Sashiko-style):
1. The return values of {vmx/svm}_interrupt_allowed() are annoying. IIUC, 0 is not allowed, 1 is allowed, and
-EBUSYis generally allowed but not right now, request immediate exit and try again? That should be documented somewhere (maybe I just missed it?).The next patch is adding the documentation, I spoke too soon :)
Quoted 5 lines
2. Aside from TDX, seems like {vmx/svm}_interrupt_allowed() are doing the same thing? So maybe move all that logic into
kvm_arch_interrupt_allowed(), rename it (because it's only used by x86), and makeinterrupt_blocked()the actual per-vendor callback?
Doesn't work, at least not with more changes/hooks, because nested_exit_on_intr() is subtly vendor specifc. The concept is identical, but the implementation needs to query vmc{b,s}12. :-/
I'm not opposed to figuring out a way to move the logic to common x86, because I agree that's where it would ideally live, but I don't want to tack that in this series.
Quoted 7 lines
Quoted 3 lines
I assume this is the case because
nested_run_pendingused to be per-vendor, but now we can clean this up. For TDX, I assume the per-vendor hook can just betdx_interrupt_allowed()reversed?It also does the renaming (nice!), so we're halfway there. I think for this series, we should at least convert all direct calls to the vendor .interrupt_allowed through the new
kvm_is_interrupt_allowed()helper.
Sadly not in this series, because handling the for_injection=true case requires moving the nested_run_pending logic to common x86, and without that, we end up with two very silly wrappers.
Quoted 2 lines
Unless you wanna go all the way and rework .interrupt_allowed to .interrupt_blocked while at it as well.
Re: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
2. Aside from TDX, seems like {vmx/svm}_interrupt_allowed() are doing the same thing? So maybe move all that logic into
kvm_arch_interrupt_allowed(), rename it (because it's only used by x86), and makeinterrupt_blocked()the actual per-vendor callback?Doesn't work, at least not with more changes/hooks, because
nested_exit_on_intr()is subtly vendor specifc. The concept is identical, but the implementation needs to query vmc{b,s}12. :-/
Duh, obvious in hindsight :)
Quoted 9 lines
I'm not opposed to figuring out a way to move the logic to common x86, because I agree that's where it would ideally live, but I don't want to tack that in this series.
Quoted 4 lines
Quoted 3 lines
I assume this is the case because
nested_run_pendingused to be per-vendor, but now we can clean this up. For TDX, I assume the per-vendor hook can just betdx_interrupt_allowed()reversed?
Quoted 7 lines
Quoted 3 lines
It also does the renaming (nice!), so we're halfway there. I think for this series, we should at least convert all direct calls to the vendor .interrupt_allowed through the new
kvm_is_interrupt_allowed()helper.Sadly not in this series, because handling the for_injection=true case requires moving the
nested_run_pendinglogic to common x86, and without that, we end up with two very silly wrappers.
Yes. I think probably the way to get there is to replace the .interrupt_allowed hook with .interrupt_blocked and a nested hook to replace nested_exit_on_intr(), but I am not sure if performance will matter here if we do two calls instead of one. With that, we can end up with most of the logic in kvm_is_interrupt_allowed().
But yeah, at this point this is definitely not something to do in this series.
Sean Christopherson
maintainerRe: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
In reply to Yosry AhmedQuoted 25 lines · Yosry Ahmed
Quoted 24 lines · Sean Christopherson
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_
nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only
kvm_vcpu_ready_for_interrupt_injection()is affected in practice.kvm_vcpu_has_events()is unreachable when a nested run is pending, as KVM clearsnested_run_pendingprior to callingkvm_emulate_halt_noskip()when putting L2 into HLT viaGUEST_ACTIVITY_HLT, and SVM has no equivalent toGUEST_ACTIVITY_STATE. I.e. the vCPU will always be runnable if a nested run is pending, and thuskvm_arch_vcpu_runnable()=>kvm_vcpu_has_events()is effectively dead code, as is__kvm_emulate_halt()=>kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly,kvm_can_do_async_pf()is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.
Quoted 6 lines
Quoted 3 lines
As for
kvm_vcpu_ready_for_interrupt_injection(), incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT."incorrectly treating interrupts as being allowed" is the status quo, that this patch fixes, not sth this patch introduces -- right?
Right.
Quoted 6 lines
The changelog reads like for the non-injection case this change might not be the right thing to do, but I don't think this is the case? I assume returning false from
kvm_vcpu_ready_for_interrupt_injection()andkvm_vcpu_has_events()if L1's interrupts are blocked while L2 is running is the right thing to do?
Yes.
Quoted 1 line
The code makes sense to me but I am trying to make sense of the changelog.
What part (parts?) is confusing? Honest question. I'm trying to reword the changelog to make it "better", but I'm failing miserable because I don't know what's wrong :-)
Re: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 5 lines
Quoted 1 line
The code makes sense to me but I am trying to make sense of the changelog.
What part (parts?) is confusing? Honest question. I'm trying to reword the changelog to make it "better", but I'm failing miserable because I don't know what's wrong :-)
1. For kvm_vcpu_has_events() being unaffected, the explanation in paragraph #3 is focused on the code path from nested_vmx_run() -> kvm_emulate_halt_noskip(). I don't immediately see how kvm_arch_vcpu_runnable() is unaffected.
2. More importantly, paragraphs #3 and #4 read like this patch would regress kvm_vcpu_ready_for_interrupt_injection() and kvm_vcpu_has_events() if it affected them. Maybe clearly state that this patch is the right thing to do for these 2 functions as well, but they are more-or-less unaffected by the bug anyway? For kvm_vcpu_ready_for_interrupt_injection(), maybe just make it more clear in paragraph #4 that it currently incorrectly treats interrupts as allowed in the problematic scenario, but it is not a problem because ..., and it only results in a spurious exit to userspace (or not even that?).
Sean Christopherson
maintainerRe: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 10 lines · Yosry Ahmed
Quoted 5 lines
Quoted 1 line
The code makes sense to me but I am trying to make sense of the changelog.
What part (parts?) is confusing? Honest question. I'm trying to reword the changelog to make it "better", but I'm failing miserable because I don't know what's wrong :-)
1. For
kvm_vcpu_has_events()being unaffected, the explanation in paragraph #3 is focused on the code path fromnested_vmx_run()->kvm_emulate_halt_noskip(). I don't immediately see howkvm_arch_vcpu_runnable()is unaffected.
To reach kvm_vcpu_has_events(), kvm_vcpu_running() needs to return false. For that to happen, vcpu->arch.mp_state needs to be something other than RUNNABLE.
If nested_run_pending is true, then mp_state *must* be RUNNABLE (barring bugs or stupid userspace), because KVM shouldn't emulate VMRUN/VMLAUNCH/VMRESUME while the vCPU is !RUNNABLE.
I didn't include that in the changelog because I thought it was obvious, but obviously (LOL) not :-D
I called out the GUEST_ACTIVITY_HLT case because (to me) that is less obvious.
Quoted 10 lines
2. More importantly, paragraphs #3 and #4 read like this patch would regress
kvm_vcpu_ready_for_interrupt_injection()andkvm_vcpu_has_events()if it affected them. Maybe clearly state that this patch is the right thing to do for these 2 functions as well, but they are more-or-less unaffected by the bug anyway? Forkvm_vcpu_ready_for_interrupt_injection(), maybe just make it more clear in paragraph #4 that it currently incorrectly treats interrupts as allowed in the problematic scenario, but it is not a problem because ..., and it only results in a spurious exit to userspace (or not even that?).
Is this better?
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_ nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.
For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only kvm_vcpu_ready_for_interrupt_injection() is affected in practice. Barring KVM bugs or misbehaving userspace (at which point all architectural guarantees are off), kvm_vcpu_has_events() is unreachable when a nested run is pending. To reach kvm_vcpu_has_events(), kvm_vcpu_running() needs to return false, i.e. vcpu->arch.mp_state needs to be something other than RUNNABLE. If nested_run_pending is true, then mp_state *must* be RUNNABLE (again barring bugs or stupid userspace), because KVM shouldn't emulate VMRUN/VMLAUNCH/VMRESUME while the vCPU is !RUNNABLE.
The one "near miss" is VMX's GUEST_ACTIVITY_STATE field, which allows L1 to put the vCPU into HLT or WFS as part of nested VMLAUNCH/VMRESUME. However, KVM clears nested_run_pending prior to calling kvm_emulate_halt_noskip() when putting L2 into HLT via GUEST_ACTIVITY_HLT, and also clears the flag before setting mp_state to INIT_RECEIVED. SVM has no equivalent to GUEST_ACTIVITY_STATE.
I.e. the vCPU will always be runnable if a nested run is pending, and thus kvm_arch_vcpu_runnable() => kvm_vcpu_has_events() is effectively dead code, as is __kvm_emulate_halt() => kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly, kvm_can_do_async_pf() is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.
As for kvm_vcpu_ready_for_interrupt_injection(), KVM's current behavior of incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT. But, KVM will still hold/block the ExtINT and request its own IRQ window. I.e. the net effect is more or less the same as the for-injection case, the unnecessary exit just happens at a different boundary.
Re: [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending
Quoted 36 lines · Sean Christopherson
Quoted 10 lines · Yosry Ahmed
Quoted 5 lines
Quoted 1 line
The code makes sense to me but I am trying to make sense of the changelog.
What part (parts?) is confusing? Honest question. I'm trying to reword the changelog to make it "better", but I'm failing miserable because I don't know what's wrong :-)
1. For
kvm_vcpu_has_events()being unaffected, the explanation in paragraph #3 is focused on the code path fromnested_vmx_run()->kvm_emulate_halt_noskip(). I don't immediately see howkvm_arch_vcpu_runnable()is unaffected.To reach
kvm_vcpu_has_events(),kvm_vcpu_running()needs to return false. For that to happen,vcpu->arch.mp_stateneeds to be something other than RUNNABLE.If
nested_run_pendingis true, thenmp_state*must* be RUNNABLE (barring bugs or stupid userspace), because KVM shouldn't emulate VMRUN/VMLAUNCH/VMRESUME while the vCPU is !RUNNABLE.I didn't include that in the changelog because I thought it was obvious, but obviously (LOL) not :-D
I called out the
GUEST_ACTIVITY_HLTcase because (to me) that is less obvious.Quoted 10 lines
2. More importantly, paragraphs #3 and #4 read like this patch would regress
kvm_vcpu_ready_for_interrupt_injection()andkvm_vcpu_has_events()if it affected them. Maybe clearly state that this patch is the right thing to do for these 2 functions as well, but they are more-or-less unaffected by the bug anyway? Forkvm_vcpu_ready_for_interrupt_injection(), maybe just make it more clear in paragraph #4 that it currently incorrectly treats interrupts as allowed in the problematic scenario, but it is not a problem because ..., and it only results in a spurious exit to userspace (or not even that?).
Quoted 1 line
Is this better?
Yes, much clearer (to me). Thanks for bearing with me :)
Quoted 42 lines
When querying whether or not interrupts (IRQs) are allowed, check for a pending nested run _after_ checking whether or not interrupts are blocked. If L1 is running L2 _without_
nested_exit_on_intr(), i.e. if L1 IRQs can be blocked while running L2, and interrupts will indeed be blocked once the nested VM-Enter to L2 is completed, then KVM should treat interrupts as not being allowed.For injection, this avoids an unnecessary (forced) VM-Exit, as KVM can immediately request an IRQ window, instead of forcing an exit and _then_ requesting an IRQ window (because after the forced exit, KVM will see that interrupts are blocked).
For non-injection usage, only
kvm_vcpu_ready_for_interrupt_injection()is affected in practice. Barring KVM bugs or misbehaving userspace (at which point all architectural guarantees are off),kvm_vcpu_has_events()is unreachable when a nested run is pending. To reachkvm_vcpu_has_events(),kvm_vcpu_running()needs to return false, i.e.vcpu->arch.mp_stateneeds to be something other than RUNNABLE. Ifnested_run_pendingis true, thenmp_state*must* be RUNNABLE (again barring bugs or stupid userspace), because KVM shouldn't emulate VMRUN/VMLAUNCH/VMRESUME while the vCPU is !RUNNABLE.The one "near miss" is VMX's
GUEST_ACTIVITY_STATEfield, which allows L1 to put the vCPU into HLT or WFS as part of nested VMLAUNCH/VMRESUME. However, KVM clearsnested_run_pendingprior to callingkvm_emulate_halt_noskip()when putting L2 into HLT viaGUEST_ACTIVITY_HLT, and also clears the flag before settingmp_statetoINIT_RECEIVED. SVM has no equivalent toGUEST_ACTIVITY_STATE.I.e. the vCPU will always be runnable if a nested run is pending, and thus
kvm_arch_vcpu_runnable()=>kvm_vcpu_has_events()is effectively dead code, as is__kvm_emulate_halt()=>kvm_vcpu_has_events(). Oh, and TDX doesn't support nested VMX. Similarly,kvm_can_do_async_pf()is unreachable as KVM shouldn't be faulting in memory with a pending nested VM-Enter.As for
kvm_vcpu_ready_for_interrupt_injection(), KVM's current behavior of incorrectly treating interrupts as being allowed could result in KVM prematurely exiting to userspace to accept an ExtINT. But, KVM will still hold/block the ExtINT and request its own IRQ window. I.e. the net effect is more or less the same as the for-injection case, the unnecessary exit just happens at a different boundary.
Sean Christopherson
maintainerPATCH v4 27/30 KVM: x86: Rework kvm_arch_interrupt_allowed() into kvm_is_interrupt_allowed()
In reply to Sean ChristophersonRename kvm_arch_interrupt_allowed() to kvm_is_interrupt_allowed() and change its return type to a boolean, as the purpose of the helper is purely to check if interrupts are architecturally allowed. I.e. whether or not interrupts are temporarily disallowed due a pending nested VM-Enter is irrelevant (and callers are most definitely not supposed to care).
Opportunistically bury the helper in x86.c, as it hasn't been referenced by arch-neutral code since commit a1b37100d9e2 ("KVM: Reduce runnability interface with arch support code"), and has long since gained _very_ x86-specific semantics (see above).
Opportunistically add a comment to call out that treating -EBUSY as "allowed" is intentional.
For all intents and purposes, no functional change intended (KVM treats -EBUSY as "allowed" before and after).
--- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/x86.c | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -2338,7 +2338,6 @@ enum {# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif -int kvm_arch_interrupt_allowed (struct kvm_vcpu *vcpu);void kvm_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);u64 kvm_scale_tsc (u64 tsc,u64 ratio);
arch/x86/kvm/x86.c +15 -8
@@ -2697,6 +2697,18 @@ static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,return 0 ; } +static bool kvm_is_interrupt_allowed (struct kvm_vcpu *vcpu) +{ +/* +* Note, .interrupt_allowed() returns -EBUSY if interrupts are allowed +* based on CPU state, but can't be immediately delivered due to a +* pending nested VM-Enter. Treat that case as "allowed", because +* the goal here is just to check if interrupts are architecturally +* allowed, not to check if they can be injected. +*/ +return kvm_x86_call (interrupt_allowed)(vcpu, false); +} +static int kvm_cpu_accept_dm_intr (struct kvm_vcpu *vcpu) {/*
@@ -2722,7 +2734,7 @@ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)* or KVM_SET_SREGS. For that to work, we must be at an * instruction boundary and with no events half-injected. */ -return (kvm_arch_interrupt_allowed (vcpu) && +return (kvm_is_interrupt_allowed (vcpu) &&kvm_cpu_accept_dm_intr (vcpu) && !kvm_event_needs_reinjection (vcpu) && !kvm_is_exception_pending (vcpu));
@@ -8472,7 +8484,7 @@ bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (kvm_test_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu))return true; -if (kvm_arch_interrupt_allowed (vcpu) &&kvm_cpu_has_interrupt (vcpu)) +if (kvm_is_interrupt_allowed (vcpu) &&kvm_cpu_has_interrupt (vcpu))return true;if (kvm_hv_has_stimer_pending (vcpu))
@@ -10308,11 +10320,6 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; } -int kvm_arch_interrupt_allowed (struct kvm_vcpu *vcpu) -{ -return kvm_x86_call (interrupt_allowed)(vcpu, false); -} -static inline u32 kvm_async_pf_hash_fn (gfn_t gfn) {BUILD_BUG_ON (!is_power_of_2 (ASYNC_PF_PER_VCPU ));
@@ -10448,7 +10455,7 @@ bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)* If interrupts are off we cannot even use an artificial * halt state. */ -return kvm_arch_interrupt_allowed (vcpu); +return kvm_is_interrupt_allowed (vcpu); } bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Sean Christopherson
maintainerPATCH v4 28/30 KVM: x86/mmu: Move kvm_arch_async_page_ready() below kvm_tdp_page_fault()
In reply to Sean ChristophersonMove the implementation of kvm_arch_async_page_ready() "down" in mmu.c so that it lives below kvm_tdp_page_fault(). This will allow moving kvm_mmu_do_page_fault() into mmu.c without needing a forward declaration.
No functional change intended.
--- arch/x86/kvm/mmu/mmu.c | 62 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
arch/x86/kvm/mmu/mmu.c +31 -31
@@ -4571,37 +4571,6 @@ static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, kvm_vcpu_gfn_to_hva(vcpu, fault->gfn), &arch); } -void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu,struct kvm_async_pf *work) -{ -int r; - -if (WARN_ON_ONCE (work->arch.error_code &PFERR_PRIVATE_ACCESS )) -return ; - -if ((vcpu->arch.mmu->root_role.direct != work->arch.direct_map) || - work->wakeup_all) -return ; - - r =kvm_mmu_reload (vcpu); -if (unlikely (r)) -return ; - -if (!vcpu->arch.mmu->root_role.direct && - work->arch.cr3 !=kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu)) -return ; - - r =kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa, work->arch.error_code, - true,NULL ,NULL ); - -/* -* Account fixed page faults, otherwise they'll never be counted, but -* ignore stats for all other return times. Page-ready "faults" aren't -* truly spurious and never trigger emulation -*/ -if (r ==RET_PF_FIXED ) - vcpu->stat.pf_fixed++; -} -static void kvm_mmu_finish_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault,int r) {
@@ -5058,6 +5027,37 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,return min (range->size, end - range->gpa); } +void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu,struct kvm_async_pf *work) +{ +int r; + +if (WARN_ON_ONCE (work->arch.error_code &PFERR_PRIVATE_ACCESS )) +return ; + +if ((vcpu->arch.mmu->root_role.direct != work->arch.direct_map) || + work->wakeup_all) +return ; + + r =kvm_mmu_reload (vcpu); +if (unlikely (r)) +return ; + +if (!vcpu->arch.mmu->root_role.direct && + work->arch.cr3 !=kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu)) +return ; + + r =kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa, work->arch.error_code, + true,NULL ,NULL ); + +/* +* Account fixed page faults, otherwise they'll never be counted, but +* ignore stats for all other return times. Page-ready "faults" aren't +* truly spurious and never trigger emulation +*/ +if (r ==RET_PF_FIXED ) + vcpu->stat.pf_fixed++; +} +#ifdef CONFIG_KVM_GUEST_MEMFD static void kvm_assert_gmem_invalidate_lock_held (struct kvm_memory_slot *slot) {
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 28/30] KVM: x86/mmu: Move kvm_arch_async_page_ready() below kvm_tdp_page_fault()
Quoted 9 lines · Sean Christopherson
Move the implementation of
kvm_arch_async_page_ready()"down" inmmu.cso that it lives belowkvm_tdp_page_fault(). This will allow movingkvm_mmu_do_page_fault()intommu.cwithout needing a forward declaration.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 29/30 KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h => mmu.c
In reply to Sean ChristophersonMove kvm_mmu_do_page_fault() into mmu.c, as there are no users outside of mmu.c, and the function typically isn't inlined by the compiler anyways. This will allow moving the EMULTYPE_xxx definitions into x86.h without having to include x86.h in mmu_internal.h, i.e. will help preserve the goal of making x86.h KVM x86's "top-level" include.
No functional change intended.
--- arch/x86/kvm/mmu/mmu.c | 67 ++++++++++++++++++++++++++++++++- arch/x86/kvm/mmu/mmu_internal.h | 66 -------------------------------- 2 files changed, 66 insertions(+), 67 deletions(-)
arch/x86/kvm/mmu/mmu.c +66 -1
@@ -4927,7 +4927,7 @@ static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu, }#endif -int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) +static int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {#ifdef CONFIG_X86_64 if (tdp_mmu_enabled)
@@ -4937,6 +4937,71 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return direct_page_fault (vcpu, fault); } +static int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa, +u64 err,bool prefetch,int *emulation_type, +u8 *level) +{ +struct kvm_page_fault fault = { + .addr = cr2_or_gpa, + .error_code = err, + .exec = err &PFERR_FETCH_MASK , + .write = err &PFERR_WRITE_MASK , + .present = err &PFERR_PRESENT_MASK , + .rsvd = err &PFERR_RSVD_MASK , + .user = err &PFERR_USER_MASK , + .prefetch = prefetch, + .is_tdp =likely (vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + .nx_huge_page_workaround_enabled = +is_nx_huge_page_enabled (vcpu->kvm), + + .max_level =KVM_MAX_HUGEPAGE_LEVEL , + .req_level =PG_LEVEL_4K , + .goal_level =PG_LEVEL_4K , + .is_private = err &PFERR_PRIVATE_ACCESS , + + .pfn =KVM_PFN_ERR_FAULT , + }; +int r; + +if (vcpu->arch.mmu->root_role.direct) { +/* +* Things like memslots don't understand the concept of a shared +* bit. Strip it so that the GFN can be used like normal, and the +* fault.addr can be used when the shared bit is needed. +*/ + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_direct_bits (vcpu->kvm); + fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); + } + +/* +* With retpoline being active an indirect call is rather expensive, +* so do a direct call in the most common case. +*/ +if (IS_ENABLED (CONFIG_MITIGATION_RETPOLINE ) && fault.is_tdp) + r =kvm_tdp_page_fault (vcpu, &fault); +else + r = vcpu->arch.mmu->page_fault (vcpu, &fault); + +/* +* Not sure what's happening, but punt to userspace and hope that +* they can fix it by changing memory to shared, or they can +* provide a better error. +*/ +if (r ==RET_PF_EMULATE && fault.is_private) { +pr_warn_ratelimited ("kvm: unexpected emulation request on private memory\n" ); +kvm_mmu_prepare_memory_fault_exit (vcpu, &fault); +return -EFAULT ; + } + +if (fault.write_fault_to_shadow_pgtable && emulation_type) + *emulation_type |=EMULTYPE_WRITE_PF_TO_SP ; +if (level) + *level = fault.goal_level; + +return r; +} + +static int kvm_tdp_page_prefault (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code,u8 *level) {
arch/x86/kvm/mmu/mmu_internal.h +0 -66
78 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 29/30] KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h => mmu.c
Quoted 11 lines · Sean Christopherson
Move
kvm_mmu_do_page_fault()intommu.c, as there are no users outside ofmmu.c, and the function typically isn't inlined by the compiler anyways. This will allow moving theEMULTYPE_xxxdefinitions intox86.hwithout having to includex86.hinmmu_internal.h, i.e. will help preserve the goal of makingx86.hKVM x86's "top-level" include.No functional change intended.
Reviewed-by Yosry Ahmed <[email protected]>Reviewed-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v4 30/30 KVM: x86: Move a pile of stuff from kvm_host.h => x86.h
In reply to Sean ChristophersonMove the majority of remaining KVM-internal declarations and defines in kvm_host.h to x86.h, so that kvm_host.h only holds structure and function definitions that need to be visible to arch-neutral KVM.
Land the emulator interfaces in x86.h, even though kvm_emulate.h *seems* like a good home, as the interfaces and defines being moved are provided by x86.c. I.e. keep kvm_emulate.h as an interface to the emulator proper.
Note, any "misses" are likely unintentional.
--- arch/x86/include/asm/kvm_host.h | 199 -------------------------------- arch/x86/kvm/ioapic.c | 1 + arch/x86/kvm/x86.h | 197 +++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 199 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -199
@@ -2074,9 +2074,6 @@ extern struct kvm_x86_ops kvm_x86_ops;#define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP#include <asm/kvm-x86-ops.h> -int kvm_x86_vendor_init (struct kvm_x86_init_ops *ops); -void kvm_x86_vendor_exit (void ); -#define __KVM_HAVE_ARCH_VM_ALLOCstatic inline struct kvm *kvm_arch_alloc_vm (void ) {
@@ -2119,175 +2116,6 @@ enum kvm_intr_type { ((vcpu) && (vcpu)->arch.handling_intr_from_guest && \ (!!in_nmi() == ((vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI))) -/* -* EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing -* userspace I/O) to indicate that the emulation context -* should be reused as is, i.e. skip initialization of -* emulation context, instruction fetch and decode. -* -* EMULTYPE_TRAP_UD - Set when emulating an intercepted #UD from hardware. -* Indicates that only select instructions (tagged with -* EmulateOnUD) should be emulated (to minimize the emulator -* attack surface). See also EMULTYPE_TRAP_UD_FORCED. -* -* EMULTYPE_SKIP - Set when emulating solely to skip an instruction, i.e. to -* decode the instruction length. For use *only* by -* kvm_x86_ops.skip_emulated_instruction() implementations if -* EMULTYPE_COMPLETE_USER_EXIT is not set. -* -* EMULTYPE_ALLOW_RETRY_PF - Set when the emulator should resume the guest to -* retry native execution under certain conditions, -* Can only be set in conjunction with EMULTYPE_PF. -* -* EMULTYPE_TRAP_UD_FORCED - Set when emulating an intercepted #UD that was -* triggered by KVM's magic "force emulation" prefix, -* which is opt in via module param (off by default). -* Bypasses EmulateOnUD restriction despite emulating -* due to an intercepted #UD (see EMULTYPE_TRAP_UD). -* Used to test the full emulator from userspace. -* -* EMULTYPE_VMWARE_GP - Set when emulating an intercepted #GP for VMware -* backdoor emulation, which is opt in via module param. -* VMware backdoor emulation handles select instructions -* and reinjects the #GP for all other cases. -* -* EMULTYPE_PF - Set when an intercepted #PF triggers the emulation, in which case -* the CR2/GPA value pass on the stack is valid. -* -* EMULTYPE_COMPLETE_USER_EXIT - Set when the emulator should update interruptibility -* state and inject single-step #DBs after skipping -* an instruction (after completing userspace I/O). -* -* EMULTYPE_WRITE_PF_TO_SP - Set when emulating an intercepted page fault that -* is attempting to write a gfn that contains one or -* more of the PTEs used to translate the write itself, -* and the owning page table is being shadowed by KVM. -* If emulation of the faulting instruction fails and -* this flag is set, KVM will exit to userspace instead -* of retrying emulation as KVM cannot make forward -* progress. -* -* If emulation fails for a write to guest page tables, -* KVM unprotects (zaps) the shadow page for the target -* gfn and resumes the guest to retry the non-emulatable -* instruction (on hardware). Unprotecting the gfn -* doesn't allow forward progress for a self-changing -* access because doing so also zaps the translation for -* the gfn, i.e. retrying the instruction will hit a -* !PRESENT fault, which results in a new shadow page -* and sends KVM back to square one. -* -* EMULTYPE_SKIP_SOFT_INT - Set in combination with EMULTYPE_SKIP to only skip -* an instruction if it could generate a given software -* interrupt, which must be encoded via -* EMULTYPE_SET_SOFT_INT_VECTOR(). - */ -#define EMULTYPE_NO_DECODE (1 << 0) -#define EMULTYPE_TRAP_UD (1 << 1) -#define EMULTYPE_SKIP (1 << 2) -#define EMULTYPE_ALLOW_RETRY_PF (1 << 3) -#define EMULTYPE_TRAP_UD_FORCED (1 << 4) -#define EMULTYPE_VMWARE_GP (1 << 5) -#define EMULTYPE_PF (1 << 6) -#define EMULTYPE_COMPLETE_USER_EXIT (1 << 7) -#define EMULTYPE_WRITE_PF_TO_SP (1 << 8) -#define EMULTYPE_SKIP_SOFT_INT (1 << 9) - -#define EMULTYPE_SET_SOFT_INT_VECTOR (v) ((u32)((v) & 0xff) << 16) -#define EMULTYPE_GET_SOFT_INT_VECTOR (e) (((e) >> 16) & 0xff) - -static inline bool kvm_can_emulate_event_vectoring (int emul_type) -{ -return !(emul_type &EMULTYPE_PF ); -} - -int kvm_emulate_instruction (struct kvm_vcpu *vcpu,int emulation_type); -int kvm_emulate_instruction_from_buffer (struct kvm_vcpu *vcpu, -void *insn,int insn_len); -void __kvm_prepare_emulation_failure_exit (struct kvm_vcpu *vcpu, -u64 *data,u8 ndata); -void kvm_prepare_emulation_failure_exit (struct kvm_vcpu *vcpu); - -void kvm_prepare_event_vectoring_exit (struct kvm_vcpu *vcpu,gpa_t gpa); -void kvm_prepare_unexpected_reason_exit (struct kvm_vcpu *vcpu,u64 exit_reason); - -int kvm_emulate_as_nop (struct kvm_vcpu *vcpu); -int kvm_emulate_invd (struct kvm_vcpu *vcpu); -int kvm_emulate_mwait (struct kvm_vcpu *vcpu); -int kvm_handle_invalid_op (struct kvm_vcpu *vcpu); -int kvm_emulate_monitor (struct kvm_vcpu *vcpu); - -int kvm_fast_pio (struct kvm_vcpu *vcpu,int size,unsigned short port,int in); -int kvm_emulate_cpuid (struct kvm_vcpu *vcpu); -int kvm_emulate_halt (struct kvm_vcpu *vcpu); -int kvm_emulate_halt_noskip (struct kvm_vcpu *vcpu); -int kvm_emulate_ap_reset_hold (struct kvm_vcpu *vcpu); -int kvm_emulate_wbinvd (struct kvm_vcpu *vcpu); - -void kvm_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector); - -int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index, -int reason,bool has_error_code,u32 error_code); - -int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr); -int kvm_emulate_xsetbv (struct kvm_vcpu *vcpu); - -int kvm_emulate_rdpmc (struct kvm_vcpu *vcpu); - -void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr); -void kvm_queue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code); -void kvm_queue_exception_p (struct kvm_vcpu *vcpu,unsigned nr,unsigned long payload); -void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned int nr, -bool has_error_code,u32 error_code); -void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault, -bool from_hardware); -void __kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu, -struct x86_exception *fault, -bool from_hardware); - -static inline void kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu, -struct x86_exception *fault) -{ -__kvm_inject_emulated_page_fault (vcpu, fault, false); -} - -bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr); - -void kvm_inject_nmi (struct kvm_vcpu *vcpu); -int kvm_get_nr_pending_nmis (struct kvm_vcpu *vcpu); - -bool kvm_apicv_activated (struct kvm *kvm); -bool kvm_vcpu_apicv_activated (struct kvm_vcpu *vcpu); -void __kvm_vcpu_update_apicv (struct kvm_vcpu *vcpu); -void __kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, -enum kvm_apicv_inhibit reason,bool set); -void kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, -enum kvm_apicv_inhibit reason,bool set); - -static inline void kvm_set_apicv_inhibit (struct kvm *kvm, -enum kvm_apicv_inhibit reason) -{ -kvm_set_or_clear_apicv_inhibit (kvm, reason, true); -} - -static inline void kvm_clear_apicv_inhibit (struct kvm *kvm, -enum kvm_apicv_inhibit reason) -{ -kvm_set_or_clear_apicv_inhibit (kvm, reason, false); -} - -void kvm_inc_or_dec_irq_window_inhibit (struct kvm *kvm,bool inc); - -static inline void kvm_inc_apicv_irq_window_req (struct kvm *kvm) -{ -kvm_inc_or_dec_irq_window_inhibit (kvm, true); -} - -static inline void kvm_dec_apicv_irq_window_req (struct kvm *kvm) -{ -kvm_inc_or_dec_irq_window_inhibit (kvm, false); -} -#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES #define kvm_arch_has_private_mem (kvm) ((kvm)->arch.has_private_mem)#endif
@@ -2304,11 +2132,6 @@ static inline unsigned long read_msr(unsigned long msr) }#endif -static inline void kvm_inject_gp (struct kvm_vcpu *vcpu,u32 error_code) -{ -kvm_queue_exception_e (vcpu,GP_VECTOR , error_code); -} -#define TSS_IOPB_BASE_OFFSET 0x66#define TSS_BASE_SIZE 0x68#define TSS_IOPB_SIZE (65536 / 8)
@@ -2338,17 +2161,6 @@ enum {# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif -void kvm_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); - -u64 kvm_scale_tsc (u64 tsc,u64 ratio); -u64 kvm_read_l1_tsc (struct kvm_vcpu *vcpu,u64 host_tsc); -u64 kvm_calc_nested_tsc_offset (u64 l1_offset,u64 l2_offset,u64 l2_multiplier); -u64 kvm_calc_nested_tsc_multiplier (u64 l1_multiplier,u64 l2_multiplier); - -void kvm_make_scan_ioapic_request (struct kvm *kvm); -void kvm_make_scan_ioapic_request_mask (struct kvm *kvm, -unsigned long *vcpu_bitmap); -bool kvm_arch_async_page_not_present (struct kvm_vcpu *vcpu,struct kvm_async_pf *work); void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
@@ -2357,15 +2169,6 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work);void kvm_arch_async_page_present_queued (struct kvm_vcpu *vcpu);bool kvm_arch_can_dequeue_async_page_present (struct kvm_vcpu *vcpu); -extern bool kvm_find_async_pf_gfn (struct kvm_vcpu *vcpu,gfn_t gfn); - -int kvm_skip_emulated_instruction (struct kvm_vcpu *vcpu); -int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err); - -void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, -u32 size); -bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu); -bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu);static inline void kvm_arch_vcpu_blocking (struct kvm_vcpu *vcpu) {
@@ -2387,8 +2190,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)#endif } -int memslot_rmap_alloc (struct kvm_memory_slot *slot,unsigned long npages); -static inline bool kvm_arch_has_irq_bypass (void ) {return enable_device_posted_irqs;
arch/x86/kvm/ioapic.c +1 -0
@@ -33,6 +33,7 @@#include "lapic.h" #include "irq.h" #include "trace.h" +#include "x86.h" static int ioapic_service (struct kvm_ioapic *vioapic,int irq,bool line_status);
arch/x86/kvm/x86.h +197 -0
231 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 30/30] KVM: x86: Move a pile of stuff from kvm_host.h => x86.h
Quoted 11 lines · Sean Christopherson
Move the majority of remaining KVM-internal declarations and defines in
kvm_host.htox86.h, so thatkvm_host.honly holds structure and function definitions that need to be visible to arch-neutral KVM.Land the emulator interfaces in
x86.h, even thoughkvm_emulate.h*seems* like a good home, as the interfaces and defines being moved are provided byx86.c. I.e. keepkvm_emulate.has an interface to the emulator proper.Note, any "misses" are likely unintentional.
Signed-off-by Sean Christopherson <[email protected]>
[...]
Quoted 4 lines
-
#defineTSS_IOPB_BASE_OFFSET0x66
#defineTSS_BASE_SIZE0x68
#defineTSS_IOPB_SIZE(65536 / 8)
Nit: Seems sashiko was right about TSS_IOPB_BASE and the related macros can be moved out of asm/kvm_host.h (ditto for the enum of TASK_SWITCH_*)?
https://lore.kernel.org/kvm/[email protected]/
But you said "majority" in the changelog, so we can always do more cleanup later. :-)
[...]
Quoted 7 lines
arch/x86/kvm/x86.h +7
-0+void __kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason,bool set); +void kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason,bool set); + +static inline void kvm_set_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason)
arch/x86/kvm/x86.h int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 548
Quoted 10 lines
arch/x86/kvm/x86.h +10
-0+{ +kvm_set_or_clear_apicv_inhibit (kvm, reason, true); +} + +static inline void kvm_clear_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason) +{ +kvm_set_or_clear_apicv_inhibit (kvm, reason, false); +} +
The definition of 'enum kvm_apicv_inhibit' remains in asm/kvm_host.h. Is it because "trace.h" still uses APICV_INHIBIT_REASONS?
Sean Christopherson
maintainerRe: [PATCH v4 30/30] KVM: x86: Move a pile of stuff from kvm_host.h => x86.h
Quoted 15 lines · Kai Huang
Quoted 11 lines · Sean Christopherson
Move the majority of remaining KVM-internal declarations and defines in
kvm_host.htox86.h, so thatkvm_host.honly holds structure and function definitions that need to be visible to arch-neutral KVM.Land the emulator interfaces in
x86.h, even thoughkvm_emulate.h*seems* like a good home, as the interfaces and defines being moved are provided byx86.c. I.e. keepkvm_emulate.has an interface to the emulator proper.Note, any "misses" are likely unintentional.
Signed-off-by Sean Christopherson <[email protected]>[...]
Quoted 7 lines
Quoted 4 lines
-
#defineTSS_IOPB_BASE_OFFSET0x66
#defineTSS_BASE_SIZE0x68
#defineTSS_IOPB_SIZE(65536 / 8)Nit: Seems sashiko was right about
TSS_IOPB_BASEand the related macros can be moved out ofasm/kvm_host.h(ditto for the enum of TASK_SWITCH_*)?
Hmm, agreed. arch/x86/kvm/tss.h seems like the perfect fit. It would be easy to slot in a patch to move these ahead of the bulk move to x86.h.
Quoted 19 lines
https://lore.kernel.org/kvm/[email protected]/
But you said "majority" in the changelog, so we can always do more cleanup later. :-)
[...]
Quoted 12 lines
arch/x86/kvm/x86.h +12
-0+void __kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason,bool set); +void kvm_set_or_clear_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason,bool set); + +static inline void kvm_set_apicv_inhibit (struct kvm *kvm, +enum kvm_apicv_inhibit reason) +{ +kvm_set_or_clear_apicv_inhibit (kvm, reason, true); +} + +static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,
Quoted 8 lines
arch/x86/kvm/x86.h int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, · line 548
Quoted 5 lines
arch/x86/kvm/x86.h +5
-0+ enum kvm_apicv_inhibit reason) +{ +kvm_set_or_clear_apicv_inhibit (kvm, reason, false); +} +The definition of 'enum
kvm_apicv_inhibit' remains inasm/kvm_host.h. Is it because "trace.h" still usesAPICV_INHIBIT_REASONS?
No, pretty sure I just overlooked the enum. I'll do another pass of kvm_host.h to see what #defines and enums can be moved.
Paolo Bonzini
maintainerQuoted 5 lines · Sean Christopherson
It's still technically spring!
Perform spring cleaning on x86.{c,h} and
asm/kvm_host.h, by addingregs.c(thekvm_cache_regs.h=>regs.his already applied) and msrs.{c,h}, and moving relevant code out ofx86.c.
Rebased and applied to kvm/queue, with the following changes:
- Collect more tags
- Move enum kvm_apicv_inhibit to x86.h
- Move TSS constants to tss.h, include it in vmx.c
- Rename kvm_x86_vcpu_ioctl_get_sregs2 to kvm_vcpu_ioctl_x86_get_sregs2
- Rename kvm_x86_vcpu_ioctl_set_sregs2 to kvm_vcpu_ioctl_x86_set_sregs2
- Fix conflicts
- Changed srcu_dereference_check() to __force cast (see 18/30)
- Adjust commit message on patches 11 and 26
Paolo
Sean Christopherson
maintainerQuoted 9 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
It's still technically spring!
Perform spring cleaning on x86.{c,h} and
asm/kvm_host.h, by addingregs.c(thekvm_cache_regs.h=>regs.his already applied) and msrs.{c,h}, and moving relevant code out ofx86.c.Rebased and applied to kvm/queue, with the following changes:
Quoted 8 lines
- Collect more tags
- Move enumkvm_apicv_inhibittox86.h
- Move TSS constants totss.h, include it invmx.c
- Renamekvm_x86_vcpu_ioctl_get_sregs2tokvm_vcpu_ioctl_x86_get_sregs2
- Renamekvm_x86_vcpu_ioctl_set_sregs2tokvm_vcpu_ioctl_x86_set_sregs2
- Fix conflicts
- Changedsrcu_dereference_check()to__forcecast (see 18/30)
- Adjust commit message on patches 11 and 26
Shoot, I have a fully refreshed version that I was going to send today (well, I was going to send it on Monday, but life happened). Any chance you want to wait and grab that? It has all of the above (except for maybe rebasing?), along with a few more cleanups.
Paolo Bonzini
maintainerQuoted 19 lines · Sean Christopherson
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
It's still technically spring!
Perform spring cleaning on x86.{c,h} and
asm/kvm_host.h, by addingregs.c(thekvm_cache_regs.h=>regs.his already applied) and msrs.{c,h}, and moving relevant code out ofx86.c.Rebased and applied to kvm/queue, with the following changes:
Quoted 8 lines
- Collect more tags
- Move enumkvm_apicv_inhibittox86.h
- Move TSS constants totss.h, include it invmx.c
- Renamekvm_x86_vcpu_ioctl_get_sregs2tokvm_vcpu_ioctl_x86_get_sregs2
- Renamekvm_x86_vcpu_ioctl_set_sregs2tokvm_vcpu_ioctl_x86_set_sregs2
- Fix conflicts
- Changedsrcu_dereference_check()to__forcecast (see 18/30)
- Adjust commit message on patches 11 and 26
Quoted 4 lines
Shoot, I have a fully refreshed version that I was going to send today (well, I was going to send it on Monday, but life happened). Any chance you want to wait and grab that? It has all of the above (except for maybe rebasing?), along with a few more cleanups.
I was expecting some kind of conflict which is why I put it only on kvm/queue. If it's possible to have them on top, that would be helpful because it means I can move what I have on kvm/next, before leaving for vacation.
Paolo
Sean Christopherson
maintainerQuoted 25 lines · Paolo Bonzini
Quoted 19 lines · Sean Christopherson
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
It's still technically spring!
Perform spring cleaning on x86.{c,h} and
asm/kvm_host.h, by addingregs.c(thekvm_cache_regs.h=>regs.his already applied) and msrs.{c,h}, and moving relevant code out ofx86.c.Rebased and applied to kvm/queue, with the following changes:
Quoted 8 lines
- Collect more tags
- Move enumkvm_apicv_inhibittox86.h
- Move TSS constants totss.h, include it invmx.c
- Renamekvm_x86_vcpu_ioctl_get_sregs2tokvm_vcpu_ioctl_x86_get_sregs2
- Renamekvm_x86_vcpu_ioctl_set_sregs2tokvm_vcpu_ioctl_x86_set_sregs2
- Fix conflicts
- Changedsrcu_dereference_check()to__forcecast (see 18/30)
- Adjust commit message on patches 11 and 26Quoted 4 lines
Shoot, I have a fully refreshed version that I was going to send today (well, I was going to send it on Monday, but life happened). Any chance you want to wait and grab that? It has all of the above (except for maybe rebasing?), along with a few more cleanups.
Quoted 4 lines
I was expecting some kind of conflict which is why I put it only on kvm/queue. If it's possible to have them on top, that would be helpful because it means I can move what I have on kvm/next, before leaving for vacation.
Roger that, I'll see how things look if I rebase on top asap, and report back.
Sean Christopherson
maintainerQuoted 26 lines · Sean Christopherson
Quoted 25 lines · Paolo Bonzini
Quoted 23 lines · Sean Christopherson
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
It's still technically spring!
Perform spring cleaning on x86.{c,h} and
asm/kvm_host.h, by addingregs.c(thekvm_cache_regs.h=>regs.his already applied) and msrs.{c,h}, and moving relevant code out ofx86.c.Rebased and applied to kvm/queue, with the following changes:
Quoted 8 lines
- Collect more tags
- Move enumkvm_apicv_inhibittox86.h
- Move TSS constants totss.h, include it invmx.c
- Renamekvm_x86_vcpu_ioctl_get_sregs2tokvm_vcpu_ioctl_x86_get_sregs2
- Renamekvm_x86_vcpu_ioctl_set_sregs2tokvm_vcpu_ioctl_x86_set_sregs2
- Fix conflicts
- Changedsrcu_dereference_check()to__forcecast (see 18/30)
- Adjust commit message on patches 11 and 26Shoot, I have a fully refreshed version that I was going to send today (well, I was going to send it on Monday, but life happened). Any chance you want to wait and grab that? It has all of the above (except for maybe rebasing?), along with a few more cleanups.
Quoted 6 lines
Quoted 4 lines
I was expecting some kind of conflict which is why I put it only on kvm/queue. If it's possible to have them on top, that would be helpful because it means I can move what I have on kvm/next, before leaving for vacation.
Roger that, I'll see how things look if I rebase on top asap, and report back.
Everything can go on top, and shouldn't generate massive conflicts, i.e. doesn't need to be rushed into kvm/next and can go through kvm-x86.
Thanks much!