target/loongarch/kvm: cpucfg and device attr fixes
v2 cover target/loongarch/kvm: cpucfg and device attr fixes
From: Tao Cui <[email protected]>
A few small robustness fixes around cpucfg register sync and kvm device attribute handling in the LoongArch KVM target.
1/4: kvm_check_cpucfg2() discarded the return value of
KVM_GET_DEVICE_ATTR and used an uninitialized val to mask cpucfg[2]; check the return value, warn and skip the mask on a GET failure (best-effort negotiation), and initialize val.
2/4: kvm_get_stealtime(), kvm_set_stealtime() and kvm_set_pv_features()
pass a struct kvm_device_attr by value to the variadic kvm_vcpu_ioctl(), which expects a pointer. Pass &attr.
3/4: drop the redundant trace_kvm_failed_get/put_cpucfg() calls and
their now-unused trace events; kvm_get/set_one_reg() already trace on failure.
4/4: kvm_loongarch_get/put_cpucfg() overwrite ret on each iteration, so
only the last register's result is returned and earlier failures are lost; on a failed read, get_cpucfg() also stores a stale val. Accumulate errors with ret |= and only store on a successful read.
Compiled and boot-tested on a loongarch64 KVM host.
Tao Cui (4):
target/loongarch/kvm: fix uninitialized val and unchecked GET in
cpucfg2 check
target/loongarch/kvm: pass device attr by reference to kvm_vcpu_ioctl target/loongarch/kvm: remove redundant cpucfg failure traces target/loongarch/kvm: fix cpucfg sync error handling
target/loongarch/kvm/kvm.c | 46 ++++++++++++++++++++--------------- target/loongarch/trace-events | 2 -- 2 files changed, 26 insertions(+), 22 deletions(-)
---
Changes in v2:
- 1/4: on KVM_GET_DEVICE_ATTR failure, warn and skip the cpucfg2 mask instead of failing the register sync; the &= mask is best-effort, and a GET failure after a successful HAS is most likely a copy_{from,to}_user issue. Switched the report from error_report() to warn_report().
Signature · 1 line
2.43.0
PATCH v2 4/4 target/loongarch/kvm: fix cpucfg sync error handling
From: Tao Cui <[email protected]>
In kvm_loongarch_get_cpucfg() and kvm_loongarch_put_cpucfg(), ret is overwritten on each iteration, so only the last register's result is returned and earlier failures are lost. On a failed read, env->cpucfg[i] is stored from a stale or uninitialized val.
Accumulate errors with ret |=, matching kvm_loongarch_get_csr()/put_csr(), and only update env->cpucfg[i] on a successful read. Keep the cpucfg2 negotiation check in put_cpucfg() on a separate variable so its early return does not overwrite the accumulated result.
--- target/loongarch/kvm/kvm.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
target/loongarch/kvm/kvm.c +9 -6
@@ -713,8 +713,11 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)CPULoongArchState *env =cpu_env (cs);for (i =0 ; i <21 ; i++) { - ret =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); - env->cpucfg[i] = (uint32_t )val; +int r =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); + ret |= r; +if (!r) { + env->cpucfg[i] = (uint32_t )val; + } }return ret; }
@@ -767,13 +770,13 @@ static int kvm_loongarch_put_cpucfg(CPUState *cs)for (i =0 ; i <21 ; i++) {if (i ==2 ) { - ret =kvm_check_cpucfg2 (cs); -if (ret) { -return ret; +int r =kvm_check_cpucfg2 (cs); +if (r) { +return r; } } val = env->cpucfg[i]; - ret =kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); + ret |=kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); }return ret; }
Signature · 1 line
2.43.0
PATCH v2 1/4 target/loongarch/kvm: fix uninitialized val and unchecked GET in cpucfg2 check
In reply to Tao CuiFrom: Tao Cui <[email protected]>
kvm_check_cpucfg2() discards the return value of KVM_GET_DEVICE_ATTR and uses the local val (the host cpucfg2 mask) without checking whether the read succeeded. val is also declared without an initializer, so on a GET failure env->cpucfg[2] &= val reads an uninitialized value.
The &= mask is best-effort feature negotiation: if KVM_HAS_DEVICE_ATTR succeeds, a GET failure is most likely a copy_{from,to}_user issue, not a reason to fail the whole register sync. Check the GET return value, warn and skip the mask on failure (the guest keeps the cpucfg2 it already has), and initialize val to 0.
--- target/loongarch/kvm/kvm.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
target/loongarch/kvm/kvm.c +12 -3
@@ -725,7 +725,7 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)static int kvm_check_cpucfg2 (CPUState *cs) {int ret; -uint64_t val; +uint64_t val =0 ;struct kvm_device_attr attr = { .group =KVM_LOONGARCH_VCPU_CPUCFG , .attr =2 ,
@@ -736,8 +736,17 @@ static int kvm_check_cpucfg2(CPUState *cs) ret =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , &attr);if (!ret) { -kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , &attr); - env->cpucfg[2 ] &= val; +/* +* The &= mask is best-effort feature negotiation. If HAS succeeded, +* a GET failure is most likely a copy_{from,to}_user issue; warn and +* keep the cpucfg2 the guest already has rather than failing the sync. +*/ +int r =kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , &attr); +if (r) { +warn_report ("CPUCFG2: KVM_GET_DEVICE_ATTR: %s" ,strerror (errno)); + }else { + env->cpucfg[2 ] &= val; + }if (FIELD_EX32 (env->cpucfg[2 ],CPUCFG2 ,FP )) {/* The FP minimal version is 1. */
Signature · 1 line
2.43.0
PATCH v2 2/4 target/loongarch/kvm: pass device attr by reference to kvm_vcpu_ioctl
In reply to Tao CuiFrom: Tao Cui <[email protected]>
kvm_vcpu_ioctl() is variadic and reads its argument as a pointer, but kvm_get_stealtime(), kvm_set_stealtime() and kvm_set_pv_features() pass the local struct kvm_device_attr by value. It currently works because of how the calling convention passes large structs; pass &attr so the argument is passed as intended.
--- target/loongarch/kvm/kvm.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
target/loongarch/kvm/kvm.c +6 -6
@@ -46,12 +46,12 @@ static int kvm_get_stealtime(CPUState *cs) .addr = (uint64_t)&env->stealtime.guest_addr, }; - err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , &attr);if (err) {return 0 ; } - err =kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , &attr);if (err) {error_report ("PVTIME: KVM_GET_DEVICE_ATTR: %s" ,strerror (errno));return err;
@@ -70,12 +70,12 @@ static int kvm_set_stealtime(CPUState *cs) .addr = (uint64_t)&env->stealtime.guest_addr, }; - err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , &attr);if (err) {return 0 ; } - err =kvm_vcpu_ioctl (cs,KVM_SET_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_SET_DEVICE_ATTR , &attr);if (err) {error_report ("PVTIME: KVM_SET_DEVICE_ATTR %s with gpa " TARGET_FMT_lx,strerror (errno), env->stealtime.guest_addr);
@@ -96,13 +96,13 @@ static int kvm_set_pv_features(CPUState *cs) .addr = (uint64_t)&val, }; - err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , &attr);if (err) {return 0 ; } val = env->pv_features; - err =kvm_vcpu_ioctl (cs,KVM_SET_DEVICE_ATTR , attr); + err =kvm_vcpu_ioctl (cs,KVM_SET_DEVICE_ATTR , &attr);if (err) {error_report ("Fail to set pv feature " TARGET_FMT_lx" with error %s" , val,strerror (errno));
Signature · 1 line
2.43.0
PATCH v2 3/4 target/loongarch/kvm: remove redundant cpucfg failure traces
In reply to Tao CuiFrom: Tao Cui <[email protected]>
kvm_get_one_reg() and kvm_set_one_reg() already trace on failure, so the trace_kvm_failed_get_cpucfg()/trace_kvm_failed_put_cpucfg() calls in kvm_loongarch_get_cpucfg() and kvm_loongarch_put_cpucfg() duplicate that. Remove the calls and the now-unused trace events.
--- target/loongarch/kvm/kvm.c | 6 ------ target/loongarch/trace-events | 2 -- 2 files changed, 8 deletions(-)
target/loongarch/kvm/kvm.c +0 -6
@@ -714,9 +714,6 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)for (i =0 ; i <21 ; i++) { ret =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); -if (ret <0 ) { -trace_kvm_failed_get_cpucfg (strerror (errno)); - } env->cpucfg[i] = (uint32_t )val; }return ret;
@@ -777,9 +774,6 @@ static int kvm_loongarch_put_cpucfg(CPUState *cs) } val = env->cpucfg[i]; ret =kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); -if (ret <0 ) { -trace_kvm_failed_put_cpucfg (strerror (errno)); - } }return ret; }
target/loongarch/trace-events +0 -2
@@ -9,7 +9,5 @@ kvm_failed_get_mpstate(const char *msg) "Failed to get mp_state from KVM: %s" kvm_failed_put_mpstate(const char *msg) "Failed to put mp_state into KVM: %s" kvm_failed_get_counter(const char *msg) "Failed to get counter from KVM: %s" kvm_failed_put_counter(const char *msg) "Failed to put counter into KVM: %s" -kvm_failed_get_cpucfg(const char *msg) "Failed to get cpucfg from KVM: %s" -kvm_failed_put_cpucfg(const char *msg) "Failed to put cpucfg into KVM: %s" kvm_arch_handle_exit(int num) "kvm arch handle exit, the reason number: %d" kvm_set_intr(int irq, int level) "kvm set interrupt, irq num: %d, level: %d"
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] target/loongarch/kvm: fix uninitialized val and unchecked GET in cpucfg2 check
In reply to Tao CuiQuoted 42 lines · Tao Cui
From: Tao Cui <[email protected]>
kvm_check_cpucfg2()discards the return value ofKVM_GET_DEVICE_ATTRand uses the local val (the host cpucfg2 mask) without checking whether the read succeeded. val is also declared without an initializer, so on a GET failure env->cpucfg[2] &= val reads an uninitialized value.The &= mask is best-effort feature negotiation: if
KVM_HAS_DEVICE_ATTRsucceeds, a GET failure is most likely a copy_{from,to}_user issue, not a reason to fail the whole register sync. Check the GET return value, warn and skip the mask on failure (the guest keeps the cpucfg2 it already has), and initialize val to 0.Signed-off-by Tao Cui <[email protected]>--- target/loongarch/kvm/kvm.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)target/loongarch/kvm/kvm.c +5
-3@@ -725,7 +725,7 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)static int kvm_check_cpucfg2 (CPUState *cs) {int ret; -uint64_t val; +uint64_t val =0 ;struct kvm_device_attr attr = { .group =KVM_LOONGARCH_VCPU_CPUCFG , .attr =2 ,@@ -736,8 +736,17 @@ static int kvm_check_cpucfg2(CPUState *cs) ret =kvm_vcpu_ioctl (cs,KVM_HAS_DEVICE_ATTR , &attr);if (!ret) { -kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , &attr); - env->cpucfg[2 ] &= val; +/* +* The &= mask is best-effort feature negotiation. If HAS succeeded, +* a GET failure is most likely a copy_{from,to}_user issue; warn and +* keep the cpucfg2 the guest already has rather than failing the sync.
target/loongarch/kvm/kvm.c static int kvm_check_cpucfg2(CPUState *cs) · line 752
Quoted 10 lines
target/loongarch/kvm/kvm.c +7
-0+*/ +int r =kvm_vcpu_ioctl (cs,KVM_GET_DEVICE_ATTR , &attr); +if (r) { +warn_report ("CPUCFG2: KVM_GET_DEVICE_ATTR: %s" ,strerror (errno)); + }else { + env->cpucfg[2 ] &= val; + }if (FIELD_EX32 (env->cpucfg[2 ],CPUCFG2 ,FP )) {/* The FP minimal version is 1. */
Re: [PATCH v2 3/4] target/loongarch/kvm: remove redundant cpucfg failure traces
In reply to Tao CuiQuoted 43 lines · Tao Cui
From: Tao Cui <[email protected]>
kvm_get_one_reg()andkvm_set_one_reg()already trace on failure, so the trace_kvm_failed_get_cpucfg()/trace_kvm_failed_put_cpucfg() calls inkvm_loongarch_get_cpucfg()andkvm_loongarch_put_cpucfg()duplicate that. Remove the calls and the now-unused trace events.Signed-off-by Tao Cui <[email protected]>--- target/loongarch/kvm/kvm.c | 6 ------ target/loongarch/trace-events | 2 -- 2 files changed, 8 deletions(-)target/loongarch/kvm/kvm.c +0
-6@@ -714,9 +714,6 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)for (i =0 ; i <21 ; i++) { ret =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); -if (ret <0 ) { -trace_kvm_failed_get_cpucfg (strerror (errno)); - } env->cpucfg[i] = (uint32_t )val; }return ret;@@ -777,9 +774,6 @@ static int kvm_loongarch_put_cpucfg(CPUState *cs) } val = env->cpucfg[i]; ret =kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); -if (ret <0 ) { -trace_kvm_failed_put_cpucfg (strerror (errno)); - } }return ret; }target/loongarch/trace-events +0
-0
target/loongarch/trace-events kvm_failed_get_mpstate(const char *msg) "Failed to get mp_state from KVM: %s" · line 13
Quoted 7 lines
target/loongarch/trace-events +0
-2@@ -9,7 +9,5 @@ kvm_failed_get_mpstate(const char *msg) "Failed to get mp_state from KVM: %s" kvm_failed_put_mpstate(const char *msg) "Failed to put mp_state into KVM: %s" kvm_failed_get_counter(const char *msg) "Failed to get counter from KVM: %s" kvm_failed_put_counter(const char *msg) "Failed to put counter into KVM: %s" -kvm_failed_get_cpucfg(const char *msg) "Failed to get cpucfg from KVM: %s" -kvm_failed_put_cpucfg(const char *msg) "Failed to put cpucfg into KVM: %s" kvm_arch_handle_exit(int num) "kvm arch handle exit, the reason number: %d" kvm_set_intr(int irq, int level) "kvm set interrupt, irq num: %d, level: %d"
Re: [PATCH v2 4/4] target/loongarch/kvm: fix cpucfg sync error handling
In reply to Tao CuiQuoted 44 lines · Tao Cui
From: Tao Cui <[email protected]>
In
kvm_loongarch_get_cpucfg()andkvm_loongarch_put_cpucfg(), ret is overwritten on each iteration, so only the last register's result is returned and earlier failures are lost. On a failed read, env->cpucfg[i] is stored from a stale or uninitialized val.Accumulate errors with ret |=, matching kvm_loongarch_get_csr()/put_csr(), and only update env->cpucfg[i] on a successful read. Keep the cpucfg2 negotiation check in
put_cpucfg()on a separate variable so its early return does not overwrite the accumulated result.Signed-off-by Tao Cui <[email protected]>--- target/loongarch/kvm/kvm.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)target/loongarch/kvm/kvm.c +6
-5@@ -713,8 +713,11 @@ static int kvm_loongarch_get_cpucfg(CPUState *cs)CPULoongArchState *env =cpu_env (cs);for (i =0 ; i <21 ; i++) { - ret =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); - env->cpucfg[i] = (uint32_t )val; +int r =kvm_get_one_reg (cs,KVM_IOC_CPUCFG (i), &val); + ret |= r; +if (!r) { + env->cpucfg[i] = (uint32_t )val; + } }return ret; }@@ -767,13 +770,13 @@ static int kvm_loongarch_put_cpucfg(CPUState *cs)for (i =0 ; i <21 ; i++) {if (i ==2 ) { - ret =kvm_check_cpucfg2 (cs); -if (ret) { -return ret; +int r =kvm_check_cpucfg2 (cs);
target/loongarch/kvm/kvm.c static int kvm_loongarch_put_cpucfg(CPUState *cs) · line 782
Quoted 10 lines
target/loongarch/kvm/kvm.c +3
-1+if (r) { +return r; } } val = env->cpucfg[i]; - ret =kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); + ret |=kvm_set_one_reg (cs,KVM_IOC_CPUCFG (i), &val); }return ret; }
gaosong
AppliedQuoted 32 lines · Tao Cui
From: Tao Cui <[email protected]>
A few small robustness fixes around cpucfg register sync and kvm device attribute handling in the LoongArch KVM target.
1/4:
kvm_check_cpucfg2()discarded the return value of
KVM_GET_DEVICE_ATTRand used an uninitialized val to mask cpucfg[2]; check the return value, warn and skip the mask on a GET failure (best-effort negotiation), and initialize val.2/4:
kvm_get_stealtime(),kvm_set_stealtime()andkvm_set_pv_features()
pass a structkvm_device_attrby value to the variadickvm_vcpu_ioctl(), which expects a pointer. Pass &attr.3/4: drop the redundant trace_kvm_failed_get/put_cpucfg() calls and
their now-unused trace events; kvm_get/set_one_reg() already trace on failure.4/4: kvm_loongarch_get/put_cpucfg() overwrite ret on each iteration, so
only the last register's result is returned and earlier failures are lost; on a failed read,get_cpucfg()also stores a stale val. Accumulate errors with ret |= and only store on a successful read.Compiled and boot-tested on a loongarch64 KVM host.
Tao Cui (4):
target/loongarch/kvm: fix uninitialized val and unchecked GET in
cpucfg2 check
target/loongarch/kvm: pass device attr by reference tokvm_vcpu_ioctltarget/loongarch/kvm: remove redundant cpucfg failure traces target/loongarch/kvm: fix cpucfg sync error handling
Quoted 5 lines
target/loongarch/kvm/kvm.c | 46 ++++++++++++++++++++--------------- target/loongarch/trace-events | 2 -- 2 files changed, 26 insertions(+), 22 deletions(-)---
Applied to loongarch-next.
Thanks.
Song Gao
Quoted 6 lines
Changes in v2:
- 1/4: onKVM_GET_DEVICE_ATTRfailure, warn and skip the cpucfg2 mask instead of failing the register sync; the &= mask is best-effort, and a GET failure after a successful HAS is most likely a copy_{from,to}_user issue. Switched the report fromerror_report()towarn_report().