Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions arch/riscv/include/asm/kvm_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ struct kvm_vcpu_arch {
/* 'static' configurations which are set only once */
struct kvm_vcpu_config cfg;

/* Indicates modified guest CSRs */
bool csr_dirty;

/* SBI steal-time accounting */
struct {
gpa_t shmem;
Expand Down
21 changes: 21 additions & 0 deletions arch/riscv/kvm/vcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define CREATE_TRACE_POINTS
#include "trace.h"

static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu);

const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
KVM_GENERIC_VCPU_STATS(),
STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
Expand Down Expand Up @@ -537,6 +539,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT);
}

vcpu->arch.csr_dirty = true;

return 0;
}

Expand Down Expand Up @@ -581,6 +585,20 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;

/*
* If VCPU is being reloaded on the same physical CPU and no
* other KVM VCPU has run on this CPU since it was last put,
* we can skip the expensive CSR and HGATP writes.
*
* Note: If a new CSR is added to this fast-path skip block,
* make sure that 'csr_dirty' is set to true in any
* ioctl (e.g., KVM_SET_ONE_REG) that modifies it.
*/
if (vcpu == __this_cpu_read(kvm_former_vcpu) &&
vcpu->arch.last_exit_cpu == cpu && !vcpu->arch.csr_dirty)
goto csr_restore_done;

vcpu->arch.csr_dirty = false;
if (kvm_riscv_nacl_sync_csr_available()) {
nsh = nacl_shmem();
nacl_csr_write(nsh, CSR_VSSTATUS, csr->vsstatus);
Expand Down Expand Up @@ -624,6 +642,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)

kvm_riscv_mmu_update_hgatp(vcpu);

csr_restore_done:
kvm_riscv_vcpu_timer_restore(vcpu);

kvm_riscv_vcpu_host_fp_save(&vcpu->arch.host_context);
Expand All @@ -645,6 +664,8 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
void *nsh;
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;

__this_cpu_write(kvm_former_vcpu, vcpu);

vcpu->cpu = -1;

kvm_riscv_vcpu_aia_put(vcpu);
Expand Down
2 changes: 2 additions & 0 deletions arch/riscv/kvm/vcpu_onereg.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu,
if (rc)
return rc;

vcpu->arch.csr_dirty = true;

return 0;
}

Expand Down