The same code is duplicated 3 times: factor a common method.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
accel/kvm/kvm-all.c | 47 ++++++++++++++++++---------------------------
1 file changed, 19 insertions(+), 28 deletions(-)
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 9060599cd73..de79f4ca099 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -2935,22 +2935,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
}
}
-static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
+static bool kvm_cpu_synchronize_put(CPUState *cpu, KvmPutState state,
+ const char *desc)
{
Error *err = NULL;
- int ret = kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE, &err);
+ int ret = kvm_arch_put_registers(cpu, state, &err);
if (ret) {
if (err) {
- error_reportf_err(err, "Restoring resisters after reset: ");
+ error_reportf_err(err, "Restoring resisters %s: ", desc);
} else {
- error_report("Failed to put registers after reset: %s",
+ error_report("Failed to put registers %s: %s", desc,
strerror(-ret));
}
- cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
- vm_stop(RUN_STATE_INTERNAL_ERROR);
+ return false;
}
cpu->vcpu_dirty = false;
+
+ return true;
+}
+
+static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
+{
+ if (kvm_cpu_synchronize_put(cpu, KVM_PUT_RESET_STATE, "after reset")) {
+ cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
+ vm_stop(RUN_STATE_INTERNAL_ERROR);
+ }
}
void kvm_cpu_synchronize_post_reset(CPUState *cpu)
@@ -2964,19 +2974,9 @@ void kvm_cpu_synchronize_post_reset(CPUState *cpu)
static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
{
- Error *err = NULL;
- int ret = kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE, &err);
- if (ret) {
- if (err) {
- error_reportf_err(err, "Putting registers after init: ");
- } else {
- error_report("Failed to put registers after init: %s",
- strerror(-ret));
- }
+ if (kvm_cpu_synchronize_put(cpu, KVM_PUT_FULL_STATE, "after init")) {
exit(1);
}
-
- cpu->vcpu_dirty = false;
}
void kvm_cpu_synchronize_post_init(CPUState *cpu)
@@ -3166,20 +3166,11 @@ int kvm_cpu_exec(CPUState *cpu)
MemTxAttrs attrs;
if (cpu->vcpu_dirty) {
- Error *err = NULL;
- ret = kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE, &err);
- if (ret) {
- if (err) {
- error_reportf_err(err, "Putting registers after init: ");
- } else {
- error_report("Failed to put registers after init: %s",
- strerror(-ret));
- }
+ if (kvm_cpu_synchronize_put(cpu, KVM_PUT_RUNTIME_STATE,
+ "at runtime")) {
ret = -1;
break;
}
-
- cpu->vcpu_dirty = false;
}
kvm_arch_pre_run(cpu, run);
--
2.51.0
On 10/7/25 01:16, Philippe Mathieu-Daudé wrote:
> The same code is duplicated 3 times: factor a common method.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
> accel/kvm/kvm-all.c | 47 ++++++++++++++++++---------------------------
> 1 file changed, 19 insertions(+), 28 deletions(-)
>
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 9060599cd73..de79f4ca099 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -2935,22 +2935,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
> }
> }
>
> -static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
> +static bool kvm_cpu_synchronize_put(CPUState *cpu, KvmPutState state,
> + const char *desc)
> {
> Error *err = NULL;
> - int ret = kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE, &err);
> + int ret = kvm_arch_put_registers(cpu, state, &err);
> if (ret) {
> if (err) {
> - error_reportf_err(err, "Restoring resisters after reset: ");
> + error_reportf_err(err, "Restoring resisters %s: ", desc);
> } else {
> - error_report("Failed to put registers after reset: %s",
> + error_report("Failed to put registers %s: %s", desc,
> strerror(-ret));
> }
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
For the to-do list: the arch routine really should be using error_setg_errno, not
deferring the test of errno to here. But it seems i386 is the only target that actually
sets errp.
r~
On Tue, Oct 07, 2025 at 10:16:16AM +0200, Philippe Mathieu-Daudé wrote:
> The same code is duplicated 3 times: factor a common method.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
> accel/kvm/kvm-all.c | 47 ++++++++++++++++++---------------------------
> 1 file changed, 19 insertions(+), 28 deletions(-)
>
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index 9060599cd73..de79f4ca099 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -2935,22 +2935,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
> }
> }
>
> -static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
> +static bool kvm_cpu_synchronize_put(CPUState *cpu, KvmPutState state,
> + const char *desc)
> {
> Error *err = NULL;
> - int ret = kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE, &err);
> + int ret = kvm_arch_put_registers(cpu, state, &err);
> if (ret) {
> if (err) {
> - error_reportf_err(err, "Restoring resisters after reset: ");
> + error_reportf_err(err, "Restoring resisters %s: ", desc);
> } else {
> - error_report("Failed to put registers after reset: %s",
> + error_report("Failed to put registers %s: %s", desc,
> strerror(-ret));
> }
> - cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
> - vm_stop(RUN_STATE_INTERNAL_ERROR);
> + return false;
> }
>
> cpu->vcpu_dirty = false;
> +
> + return true;
> +}
> +
> +static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
> +{
> + if (kvm_cpu_synchronize_put(cpu, KVM_PUT_RESET_STATE, "after reset")) {
This should be !kvm_cpu_synchronize_put() and same comment for the other
calls below.
Thanks,
drew
> + cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
> + vm_stop(RUN_STATE_INTERNAL_ERROR);
> + }
> }
>
> void kvm_cpu_synchronize_post_reset(CPUState *cpu)
> @@ -2964,19 +2974,9 @@ void kvm_cpu_synchronize_post_reset(CPUState *cpu)
>
> static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
> {
> - Error *err = NULL;
> - int ret = kvm_arch_put_registers(cpu, KVM_PUT_FULL_STATE, &err);
> - if (ret) {
> - if (err) {
> - error_reportf_err(err, "Putting registers after init: ");
> - } else {
> - error_report("Failed to put registers after init: %s",
> - strerror(-ret));
> - }
> + if (kvm_cpu_synchronize_put(cpu, KVM_PUT_FULL_STATE, "after init")) {
> exit(1);
> }
> -
> - cpu->vcpu_dirty = false;
> }
>
> void kvm_cpu_synchronize_post_init(CPUState *cpu)
> @@ -3166,20 +3166,11 @@ int kvm_cpu_exec(CPUState *cpu)
> MemTxAttrs attrs;
>
> if (cpu->vcpu_dirty) {
> - Error *err = NULL;
> - ret = kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE, &err);
> - if (ret) {
> - if (err) {
> - error_reportf_err(err, "Putting registers after init: ");
> - } else {
> - error_report("Failed to put registers after init: %s",
> - strerror(-ret));
> - }
> + if (kvm_cpu_synchronize_put(cpu, KVM_PUT_RUNTIME_STATE,
> + "at runtime")) {
> ret = -1;
> break;
> }
> -
> - cpu->vcpu_dirty = false;
> }
>
> kvm_arch_pre_run(cpu, run);
> --
> 2.51.0
>
>
On 7/10/25 15:30, Andrew Jones wrote:
> On Tue, Oct 07, 2025 at 10:16:16AM +0200, Philippe Mathieu-Daudé wrote:
>> The same code is duplicated 3 times: factor a common method.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>> accel/kvm/kvm-all.c | 47 ++++++++++++++++++---------------------------
>> 1 file changed, 19 insertions(+), 28 deletions(-)
>>
>> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
>> index 9060599cd73..de79f4ca099 100644
>> --- a/accel/kvm/kvm-all.c
>> +++ b/accel/kvm/kvm-all.c
>> @@ -2935,22 +2935,32 @@ void kvm_cpu_synchronize_state(CPUState *cpu)
>> }
>> }
>>
>> -static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
>> +static bool kvm_cpu_synchronize_put(CPUState *cpu, KvmPutState state,
>> + const char *desc)
>> {
>> Error *err = NULL;
>> - int ret = kvm_arch_put_registers(cpu, KVM_PUT_RESET_STATE, &err);
>> + int ret = kvm_arch_put_registers(cpu, state, &err);
>> if (ret) {
>> if (err) {
>> - error_reportf_err(err, "Restoring resisters after reset: ");
>> + error_reportf_err(err, "Restoring resisters %s: ", desc);
>> } else {
>> - error_report("Failed to put registers after reset: %s",
>> + error_report("Failed to put registers %s: %s", desc,
>> strerror(-ret));
>> }
>> - cpu_dump_state(cpu, stderr, CPU_DUMP_CODE);
>> - vm_stop(RUN_STATE_INTERNAL_ERROR);
>> + return false;
>> }
>>
>> cpu->vcpu_dirty = false;
>> +
>> + return true;
>> +}
>> +
>> +static void do_kvm_cpu_synchronize_post_reset(CPUState *cpu, run_on_cpu_data arg)
>> +{
>> + if (kvm_cpu_synchronize_put(cpu, KVM_PUT_RESET_STATE, "after reset")) {
>
> This should be !kvm_cpu_synchronize_put() and same comment for the other
> calls below.
Oops! Thanks :)
>
> Thanks,
> drew
© 2016 - 2026 Red Hat, Inc.