Skip to content

Commit 0f68fe4

Browse files
committed
Merge tag 'kvm-x86-vmx-6.18' of https://github.com/kvm-x86/linux into HEAD
KVM VMX changes for 6.18 - Add read/write helpers for MSRs that need to be accessed with preemption disable to prepare for virtualizing FRED RSP0. - Fix a bug where KVM would return 0/success from __tdx_bringup() on error, i.e. where KVM would load with enable_tdx=true despite TDX not being usable. - Minor cleanups.
2 parents 5b0d0d8 + 510c47f commit 0f68fe4

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,7 +2491,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
24912491
/* TDVPS = TDVPR(4K page) + TDCX(multiple 4K pages), -1 for TDVPR. */
24922492
kvm_tdx->td.tdcx_nr_pages = tdx_sysinfo->td_ctrl.tdvps_base_size / PAGE_SIZE - 1;
24932493
tdcs_pages = kcalloc(kvm_tdx->td.tdcs_nr_pages, sizeof(*kvm_tdx->td.tdcs_pages),
2494-
GFP_KERNEL | __GFP_ZERO);
2494+
GFP_KERNEL);
24952495
if (!tdcs_pages)
24962496
goto free_tdr;
24972497

@@ -3471,12 +3471,11 @@ static int __init __tdx_bringup(void)
34713471
if (r)
34723472
goto tdx_bringup_err;
34733473

3474+
r = -EINVAL;
34743475
/* Get TDX global information for later use */
34753476
tdx_sysinfo = tdx_get_sysinfo();
3476-
if (WARN_ON_ONCE(!tdx_sysinfo)) {
3477-
r = -EINVAL;
3477+
if (WARN_ON_ONCE(!tdx_sysinfo))
34783478
goto get_sysinfo_err;
3479-
}
34803479

34813480
/* Check TDX module and KVM capabilities */
34823481
if (!tdx_get_supported_attrs(&tdx_sysinfo->td_conf) ||
@@ -3519,14 +3518,11 @@ static int __init __tdx_bringup(void)
35193518
if (td_conf->max_vcpus_per_td < num_present_cpus()) {
35203519
pr_err("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n",
35213520
td_conf->max_vcpus_per_td, num_present_cpus());
3522-
r = -EINVAL;
35233521
goto get_sysinfo_err;
35243522
}
35253523

3526-
if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) {
3527-
r = -EINVAL;
3524+
if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids()))
35283525
goto get_sysinfo_err;
3529-
}
35303526

35313527
/*
35323528
* Leave hardware virtualization enabled after TDX is enabled

arch/x86/kvm/vmx/vmx.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,22 +1344,35 @@ static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
13441344
}
13451345

13461346
#ifdef CONFIG_X86_64
1347-
static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
1347+
static u64 vmx_read_guest_host_msr(struct vcpu_vmx *vmx, u32 msr, u64 *cache)
13481348
{
13491349
preempt_disable();
13501350
if (vmx->vt.guest_state_loaded)
1351-
rdmsrq(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1351+
*cache = read_msr(msr);
13521352
preempt_enable();
1353-
return vmx->msr_guest_kernel_gs_base;
1353+
return *cache;
13541354
}
13551355

1356-
static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1356+
static void vmx_write_guest_host_msr(struct vcpu_vmx *vmx, u32 msr, u64 data,
1357+
u64 *cache)
13571358
{
13581359
preempt_disable();
13591360
if (vmx->vt.guest_state_loaded)
1360-
wrmsrq(MSR_KERNEL_GS_BASE, data);
1361+
wrmsrns(msr, data);
13611362
preempt_enable();
1362-
vmx->msr_guest_kernel_gs_base = data;
1363+
*cache = data;
1364+
}
1365+
1366+
static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
1367+
{
1368+
return vmx_read_guest_host_msr(vmx, MSR_KERNEL_GS_BASE,
1369+
&vmx->msr_guest_kernel_gs_base);
1370+
}
1371+
1372+
static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1373+
{
1374+
vmx_write_guest_host_msr(vmx, MSR_KERNEL_GS_BASE, data,
1375+
&vmx->msr_guest_kernel_gs_base);
13631376
}
13641377
#endif
13651378

@@ -8532,7 +8545,9 @@ __init int vmx_hardware_setup(void)
85328545
*/
85338546
if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
85348547
kvm_caps.supported_quirks &= ~KVM_X86_QUIRK_IGNORE_GUEST_PAT;
8535-
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_IGNORE_GUEST_PAT;
8548+
8549+
kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_IGNORE_GUEST_PAT;
8550+
85368551
return r;
85378552
}
85388553

0 commit comments

Comments
 (0)