Skip to content

Commit 41f4d0c

Browse files
atishp04avpatel
authored andcommitted
RISC-V: KVM: No need of explicit writable slot check
There is not much value in checking if a memslot is writable explicitly before a write as it may change underneath after the check. Rather, return invalid address error when write_guest fails as it checks if the slot is writable anyways. Suggested-by: Sean Christopherson <[email protected]> Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]> Acked-by: Paul Walmsley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
1 parent 880fcc3 commit 41f4d0c

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

arch/riscv/kvm/vcpu_pmu.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,6 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
409409
int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data);
410410
int sbiret = 0;
411411
gpa_t saddr;
412-
unsigned long hva;
413-
bool writable;
414412

415413
if (!kvpmu || flags) {
416414
sbiret = SBI_ERR_INVALID_PARAM;
@@ -432,19 +430,14 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
432430
goto out;
433431
}
434432

435-
hva = kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writable);
436-
if (kvm_is_error_hva(hva) || !writable) {
437-
sbiret = SBI_ERR_INVALID_ADDRESS;
438-
goto out;
439-
}
440-
441433
kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
442434
if (!kvpmu->sdata)
443435
return -ENOMEM;
444436

437+
/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
445438
if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
446439
kfree(kvpmu->sdata);
447-
sbiret = SBI_ERR_FAILURE;
440+
sbiret = SBI_ERR_INVALID_ADDRESS;
448441
goto out;
449442
}
450443

arch/riscv/kvm/vcpu_sbi_sta.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
8585
unsigned long shmem_phys_hi = cp->a1;
8686
u32 flags = cp->a2;
8787
struct sbi_sta_struct zero_sta = {0};
88-
unsigned long hva;
89-
bool writable;
9088
gpa_t shmem;
9189
int ret;
9290

@@ -111,13 +109,10 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
111109
return SBI_ERR_INVALID_ADDRESS;
112110
}
113111

114-
hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
115-
if (kvm_is_error_hva(hva) || !writable)
116-
return SBI_ERR_INVALID_ADDRESS;
117-
112+
/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
118113
ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
119114
if (ret)
120-
return SBI_ERR_FAILURE;
115+
return SBI_ERR_INVALID_ADDRESS;
121116

122117
vcpu->arch.sta.shmem = shmem;
123118
vcpu->arch.sta.last_steal = current->sched_info.run_delay;

0 commit comments

Comments
 (0)