Skip to content

Commit a53071b

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull KVM fixes from Paolo Bonzini: "x86 bug fixes" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: X86: Narrow down the IPI fastpath to single target IPI KVM: LAPIC: Also cancel preemption timer when disarm LAPIC timer KVM: VMX: don't allow memory operands for inline asm that modifies SP KVM: LAPIC: Mark hrtimer for period or oneshot mode to expire in hard interrupt context KVM: SVM: Issue WBINVD after deactivating an SEV guest KVM: SVM: document KVM_MEM_ENCRYPT_OP, let userspace detect if SEV is available KVM: x86: remove bogus user-triggerable WARN_ON
2 parents 23cb849 + e1be9ac commit a53071b

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

Documentation/virt/kvm/amd-memory-encryption.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ key management interface to perform common hypervisor activities such as
5353
encrypting bootstrap code, snapshot, migrating and debugging the guest. For more
5454
information, see the SEV Key Management spec [api-spec]_
5555

56+
The main ioctl to access SEV is KVM_MEM_ENCRYPT_OP. If the argument
57+
to KVM_MEM_ENCRYPT_OP is NULL, the ioctl returns 0 if SEV is enabled
58+
and ``ENOTTY` if it is disabled (on some older versions of Linux,
59+
the ioctl runs normally even with a NULL argument, and therefore will
60+
likely return ``EFAULT``). If non-NULL, the argument to KVM_MEM_ENCRYPT_OP
61+
must be a struct kvm_sev_cmd::
62+
63+
struct kvm_sev_cmd {
64+
__u32 id;
65+
__u64 data;
66+
__u32 error;
67+
__u32 sev_fd;
68+
};
69+
70+
71+
The ``id`` field contains the subcommand, and the ``data`` field points to
72+
another struct containing arguments specific to command. The ``sev_fd``
73+
should point to a file descriptor that is opened on the ``/dev/sev``
74+
device, if needed (see individual commands).
75+
76+
On output, ``error`` is zero on success, or an error code. Error codes
77+
are defined in ``<linux/psp-dev.h>`.
78+
5679
KVM implements the following commands to support common lifecycle events of SEV
5780
guests, such as launching, running, snapshotting, migrating and decommissioning.
5881
@@ -90,6 +113,8 @@ Returns: 0 on success, -negative on error
90113
91114
On success, the 'handle' field contains a new handle and on error, a negative value.
92115
116+
KVM_SEV_LAUNCH_START requires the ``sev_fd`` field to be valid.
117+
93118
For more details, see SEV spec Section 6.2.
94119

95120
3. KVM_SEV_LAUNCH_UPDATE_DATA

arch/x86/kvm/lapic.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,8 @@ static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
14451445
}
14461446
}
14471447

1448+
static void cancel_hv_timer(struct kvm_lapic *apic);
1449+
14481450
static void apic_update_lvtt(struct kvm_lapic *apic)
14491451
{
14501452
u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
@@ -1454,6 +1456,10 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
14541456
if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
14551457
APIC_LVT_TIMER_TSCDEADLINE)) {
14561458
hrtimer_cancel(&apic->lapic_timer.timer);
1459+
preempt_disable();
1460+
if (apic->lapic_timer.hv_timer_in_use)
1461+
cancel_hv_timer(apic);
1462+
preempt_enable();
14571463
kvm_lapic_set_reg(apic, APIC_TMICT, 0);
14581464
apic->lapic_timer.period = 0;
14591465
apic->lapic_timer.tscdeadline = 0;
@@ -1715,7 +1721,7 @@ static void start_sw_period(struct kvm_lapic *apic)
17151721

17161722
hrtimer_start(&apic->lapic_timer.timer,
17171723
apic->lapic_timer.target_expiration,
1718-
HRTIMER_MODE_ABS);
1724+
HRTIMER_MODE_ABS_HARD);
17191725
}
17201726

17211727
bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)

arch/x86/kvm/svm.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,14 +1933,6 @@ static void sev_clflush_pages(struct page *pages[], unsigned long npages)
19331933
static void __unregister_enc_region_locked(struct kvm *kvm,
19341934
struct enc_region *region)
19351935
{
1936-
/*
1937-
* The guest may change the memory encryption attribute from C=0 -> C=1
1938-
* or vice versa for this memory range. Lets make sure caches are
1939-
* flushed to ensure that guest data gets written into memory with
1940-
* correct C-bit.
1941-
*/
1942-
sev_clflush_pages(region->pages, region->npages);
1943-
19441936
sev_unpin_memory(kvm, region->pages, region->npages);
19451937
list_del(&region->list);
19461938
kfree(region);
@@ -1970,6 +1962,13 @@ static void sev_vm_destroy(struct kvm *kvm)
19701962

19711963
mutex_lock(&kvm->lock);
19721964

1965+
/*
1966+
* Ensure that all guest tagged cache entries are flushed before
1967+
* releasing the pages back to the system for use. CLFLUSH will
1968+
* not do this, so issue a WBINVD.
1969+
*/
1970+
wbinvd_on_all_cpus();
1971+
19731972
/*
19741973
* if userspace was terminated before unregistering the memory regions
19751974
* then lets unpin all the registered memory.
@@ -7158,6 +7157,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
71587157
if (!svm_sev_enabled())
71597158
return -ENOTTY;
71607159

7160+
if (!argp)
7161+
return 0;
7162+
71617163
if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
71627164
return -EFAULT;
71637165

@@ -7285,6 +7287,13 @@ static int svm_unregister_enc_region(struct kvm *kvm,
72857287
goto failed;
72867288
}
72877289

7290+
/*
7291+
* Ensure that all guest tagged cache entries are flushed before
7292+
* releasing the pages back to the system for use. CLFLUSH will
7293+
* not do this, so issue a WBINVD.
7294+
*/
7295+
wbinvd_on_all_cpus();
7296+
72887297
__unregister_enc_region_locked(kvm, region);
72897298

72907299
mutex_unlock(&kvm->lock);

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6287,7 +6287,7 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
62876287
#endif
62886288
ASM_CALL_CONSTRAINT
62896289
:
6290-
THUNK_TARGET(entry),
6290+
[thunk_target]"r"(entry),
62916291
[ss]"i"(__KERNEL_DS),
62926292
[cs]"i"(__KERNEL_CS)
62936293
);

arch/x86/kvm/x86.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,10 @@ EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
15541554
*/
15551555
static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data)
15561556
{
1557-
if (lapic_in_kernel(vcpu) && apic_x2apic_mode(vcpu->arch.apic) &&
1557+
if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic))
1558+
return 1;
1559+
1560+
if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
15581561
((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
15591562
((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
15601563

@@ -2444,7 +2447,6 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
24442447
vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
24452448
vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
24462449
vcpu->last_guest_tsc = tsc_timestamp;
2447-
WARN_ON((s64)vcpu->hv_clock.system_time < 0);
24482450

24492451
/* If the host uses TSC clocksource, then it is stable */
24502452
pvclock_flags = 0;

0 commit comments

Comments
 (0)