Skip to content

Commit cd82c4a

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull more x86 kvm fixes from Paolo Bonzini: - Cache coherency fix for SEV live migration - Fix for instruction emulation with PKU - fixes for rare delaying of interrupt delivery - fix for SEV-ES buffer overflow * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: SEV-ES: go over the sev_pio_data buffer in multiple passes if needed KVM: SEV-ES: keep INS functions together KVM: x86: remove unnecessary arguments from complete_emulator_pio_in KVM: x86: split the two parts of emulator_pio_in KVM: SEV-ES: clean up kvm_sev_es_ins/outs KVM: x86: leave vcpu->arch.pio.count alone in emulator_pio_in_out KVM: SEV-ES: rename guest_ins_data to sev_pio_data KVM: SEV: Flush cache on non-coherent systems before RECEIVE_UPDATE_DATA KVM: MMU: Reset mmu->pkru_mask to avoid stale data KVM: nVMX: promptly process interrupts delivered while in guest mode KVM: x86: check for interrupts before deciding whether to exit the fast path
2 parents 6422251 + 95e16b4 commit cd82c4a

File tree

5 files changed

+121
-62
lines changed

5 files changed

+121
-62
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,8 @@ struct kvm_vcpu_arch {
702702

703703
struct kvm_pio_request pio;
704704
void *pio_data;
705-
void *guest_ins_data;
705+
void *sev_pio_data;
706+
unsigned sev_pio_count;
706707

707708
u8 event_exit_inst_len;
708709

arch/x86/kvm/mmu/mmu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4596,10 +4596,10 @@ static void update_pkru_bitmask(struct kvm_mmu *mmu)
45964596
unsigned bit;
45974597
bool wp;
45984598

4599-
if (!is_cr4_pke(mmu)) {
4600-
mmu->pkru_mask = 0;
4599+
mmu->pkru_mask = 0;
4600+
4601+
if (!is_cr4_pke(mmu))
46014602
return;
4602-
}
46034603

46044604
wp = is_cr0_wp(mmu);
46054605

arch/x86/kvm/svm/sev.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,13 @@ static int sev_receive_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
14841484
goto e_free_trans;
14851485
}
14861486

1487+
/*
1488+
* Flush (on non-coherent CPUs) before RECEIVE_UPDATE_DATA, the PSP
1489+
* encrypts the written data with the guest's key, and the cache may
1490+
* contain dirty, unencrypted data.
1491+
*/
1492+
sev_clflush_pages(guest_page, n);
1493+
14871494
/* The RECEIVE_UPDATE_DATA command requires C-bit to be always set. */
14881495
data.guest_address = (page_to_pfn(guest_page[0]) << PAGE_SHIFT) + offset;
14891496
data.guest_address |= sev_me_mask;

arch/x86/kvm/vmx/vmx.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6305,18 +6305,13 @@ static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
63056305

63066306
/*
63076307
* If we are running L2 and L1 has a new pending interrupt
6308-
* which can be injected, we should re-evaluate
6309-
* what should be done with this new L1 interrupt.
6310-
* If L1 intercepts external-interrupts, we should
6311-
* exit from L2 to L1. Otherwise, interrupt should be
6312-
* delivered directly to L2.
6308+
* which can be injected, this may cause a vmexit or it may
6309+
* be injected into L2. Either way, this interrupt will be
6310+
* processed via KVM_REQ_EVENT, not RVI, because we do not use
6311+
* virtual interrupt delivery to inject L1 interrupts into L2.
63136312
*/
6314-
if (is_guest_mode(vcpu) && max_irr_updated) {
6315-
if (nested_exit_on_intr(vcpu))
6316-
kvm_vcpu_exiting_guest_mode(vcpu);
6317-
else
6318-
kvm_make_request(KVM_REQ_EVENT, vcpu);
6319-
}
6313+
if (is_guest_mode(vcpu) && max_irr_updated)
6314+
kvm_make_request(KVM_REQ_EVENT, vcpu);
63206315
} else {
63216316
max_irr = kvm_lapic_find_highest_irr(vcpu);
63226317
}

arch/x86/kvm/x86.c

Lines changed: 103 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6906,18 +6906,16 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
69066906
}
69076907

69086908
static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6909-
unsigned short port, void *val,
6909+
unsigned short port,
69106910
unsigned int count, bool in)
69116911
{
69126912
vcpu->arch.pio.port = port;
69136913
vcpu->arch.pio.in = in;
69146914
vcpu->arch.pio.count = count;
69156915
vcpu->arch.pio.size = size;
69166916

6917-
if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
6918-
vcpu->arch.pio.count = 0;
6917+
if (!kernel_pio(vcpu, vcpu->arch.pio_data))
69196918
return 1;
6920-
}
69216919

69226920
vcpu->run->exit_reason = KVM_EXIT_IO;
69236921
vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
@@ -6929,26 +6927,39 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
69296927
return 0;
69306928
}
69316929

6932-
static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6933-
unsigned short port, void *val, unsigned int count)
6930+
static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6931+
unsigned short port, unsigned int count)
69346932
{
6935-
int ret;
6933+
WARN_ON(vcpu->arch.pio.count);
6934+
memset(vcpu->arch.pio_data, 0, size * count);
6935+
return emulator_pio_in_out(vcpu, size, port, count, true);
6936+
}
69366937

6937-
if (vcpu->arch.pio.count)
6938-
goto data_avail;
6938+
static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, void *val)
6939+
{
6940+
int size = vcpu->arch.pio.size;
6941+
unsigned count = vcpu->arch.pio.count;
6942+
memcpy(val, vcpu->arch.pio_data, size * count);
6943+
trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data);
6944+
vcpu->arch.pio.count = 0;
6945+
}
69396946

6940-
memset(vcpu->arch.pio_data, 0, size * count);
6947+
static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6948+
unsigned short port, void *val, unsigned int count)
6949+
{
6950+
if (vcpu->arch.pio.count) {
6951+
/* Complete previous iteration. */
6952+
} else {
6953+
int r = __emulator_pio_in(vcpu, size, port, count);
6954+
if (!r)
6955+
return r;
69416956

6942-
ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6943-
if (ret) {
6944-
data_avail:
6945-
memcpy(val, vcpu->arch.pio_data, size * count);
6946-
trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
6947-
vcpu->arch.pio.count = 0;
6948-
return 1;
6957+
/* Results already available, fall through. */
69496958
}
69506959

6951-
return 0;
6960+
WARN_ON(count != vcpu->arch.pio.count);
6961+
complete_emulator_pio_in(vcpu, val);
6962+
return 1;
69526963
}
69536964

69546965
static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
@@ -6963,9 +6974,15 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
69636974
unsigned short port, const void *val,
69646975
unsigned int count)
69656976
{
6977+
int ret;
6978+
69666979
memcpy(vcpu->arch.pio_data, val, size * count);
69676980
trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
6968-
return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
6981+
ret = emulator_pio_in_out(vcpu, size, port, count, false);
6982+
if (ret)
6983+
vcpu->arch.pio.count = 0;
6984+
6985+
return ret;
69696986
}
69706987

69716988
static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
@@ -9643,14 +9660,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
96439660
if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST))
96449661
break;
96459662

9646-
if (unlikely(kvm_vcpu_exit_request(vcpu))) {
9663+
if (vcpu->arch.apicv_active)
9664+
static_call(kvm_x86_sync_pir_to_irr)(vcpu);
9665+
9666+
if (unlikely(kvm_vcpu_exit_request(vcpu))) {
96479667
exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
96489668
break;
96499669
}
9650-
9651-
if (vcpu->arch.apicv_active)
9652-
static_call(kvm_x86_sync_pir_to_irr)(vcpu);
9653-
}
9670+
}
96549671

96559672
/*
96569673
* Do this here before restoring debug registers on the host. And
@@ -12368,53 +12385,92 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,
1236812385
}
1236912386
EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);
1237012387

12371-
static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
12388+
static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,
12389+
unsigned int port);
12390+
12391+
static int complete_sev_es_emulated_outs(struct kvm_vcpu *vcpu)
1237212392
{
12373-
memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data,
12374-
vcpu->arch.pio.count * vcpu->arch.pio.size);
12375-
vcpu->arch.pio.count = 0;
12393+
int size = vcpu->arch.pio.size;
12394+
int port = vcpu->arch.pio.port;
1237612395

12396+
vcpu->arch.pio.count = 0;
12397+
if (vcpu->arch.sev_pio_count)
12398+
return kvm_sev_es_outs(vcpu, size, port);
1237712399
return 1;
1237812400
}
1237912401

1238012402
static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,
12381-
unsigned int port, void *data, unsigned int count)
12403+
unsigned int port)
1238212404
{
12383-
int ret;
12384-
12385-
ret = emulator_pio_out_emulated(vcpu->arch.emulate_ctxt, size, port,
12386-
data, count);
12387-
if (ret)
12388-
return ret;
12405+
for (;;) {
12406+
unsigned int count =
12407+
min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count);
12408+
int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count);
12409+
12410+
/* memcpy done already by emulator_pio_out. */
12411+
vcpu->arch.sev_pio_count -= count;
12412+
vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size;
12413+
if (!ret)
12414+
break;
1238912415

12390-
vcpu->arch.pio.count = 0;
12416+
/* Emulation done by the kernel. */
12417+
if (!vcpu->arch.sev_pio_count)
12418+
return 1;
12419+
}
1239112420

12421+
vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs;
1239212422
return 0;
1239312423
}
1239412424

1239512425
static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,
12396-
unsigned int port, void *data, unsigned int count)
12426+
unsigned int port);
12427+
12428+
static void advance_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
1239712429
{
12398-
int ret;
12430+
unsigned count = vcpu->arch.pio.count;
12431+
complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data);
12432+
vcpu->arch.sev_pio_count -= count;
12433+
vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size;
12434+
}
1239912435

12400-
ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port,
12401-
data, count);
12402-
if (ret) {
12403-
vcpu->arch.pio.count = 0;
12404-
} else {
12405-
vcpu->arch.guest_ins_data = data;
12406-
vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
12436+
static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu)
12437+
{
12438+
int size = vcpu->arch.pio.size;
12439+
int port = vcpu->arch.pio.port;
12440+
12441+
advance_sev_es_emulated_ins(vcpu);
12442+
if (vcpu->arch.sev_pio_count)
12443+
return kvm_sev_es_ins(vcpu, size, port);
12444+
return 1;
12445+
}
12446+
12447+
static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,
12448+
unsigned int port)
12449+
{
12450+
for (;;) {
12451+
unsigned int count =
12452+
min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count);
12453+
if (!__emulator_pio_in(vcpu, size, port, count))
12454+
break;
12455+
12456+
/* Emulation done by the kernel. */
12457+
advance_sev_es_emulated_ins(vcpu);
12458+
if (!vcpu->arch.sev_pio_count)
12459+
return 1;
1240712460
}
1240812461

12462+
vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
1240912463
return 0;
1241012464
}
1241112465

1241212466
int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
1241312467
unsigned int port, void *data, unsigned int count,
1241412468
int in)
1241512469
{
12416-
return in ? kvm_sev_es_ins(vcpu, size, port, data, count)
12417-
: kvm_sev_es_outs(vcpu, size, port, data, count);
12470+
vcpu->arch.sev_pio_data = data;
12471+
vcpu->arch.sev_pio_count = count;
12472+
return in ? kvm_sev_es_ins(vcpu, size, port)
12473+
: kvm_sev_es_outs(vcpu, size, port);
1241812474
}
1241912475
EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
1242012476

0 commit comments

Comments
 (0)