[PATCH v2 02/32] hw/accel: add a per-accelerator callback to change VM accelerator handle

Ani Sinha posted 32 patches 4 weeks ago
[PATCH v2 02/32] hw/accel: add a per-accelerator callback to change VM accelerator handle
Posted by Ani Sinha 4 weeks ago
When a confidential virtual machine is reset, a new guest context in the
accelerator must be generated post reset. Therefore, the old accelerator guest
file handle must closed and a new one created. To this end, a per-accelerator
callback, "reset_vmfd" is introduced that would get called when a confidential
guest is reset. Subsequent patches will introduce specific implementation of
this callback for KVM accelerator.

Signed-off-by: Ani Sinha <anisinha@redhat.com>
---
 include/accel/accel-ops.h |  1 +
 system/runstate.c         | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/accel/accel-ops.h b/include/accel/accel-ops.h
index 23a8c246e1..998a95ca69 100644
--- a/include/accel/accel-ops.h
+++ b/include/accel/accel-ops.h
@@ -23,6 +23,7 @@ struct AccelClass {
     AccelOpsClass *ops;
 
     int (*init_machine)(AccelState *as, MachineState *ms);
+    int (*reset_vmfd)(MachineState *ms);
     bool (*cpu_common_realize)(CPUState *cpu, Error **errp);
     void (*cpu_common_unrealize)(CPUState *cpu);
     /* get_stats: Append statistics to @buf */
diff --git a/system/runstate.c b/system/runstate.c
index ed2db56480..b0ce0410fa 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -42,6 +42,7 @@
 #include "qapi/qapi-commands-run-state.h"
 #include "qapi/qapi-events-run-state.h"
 #include "qemu/accel.h"
+#include "accel/accel-ops.h"
 #include "qemu/error-report.h"
 #include "qemu/job.h"
 #include "qemu/log.h"
@@ -508,6 +509,8 @@ void qemu_system_reset(ShutdownCause reason)
 {
     MachineClass *mc;
     ResetType type;
+    AccelClass *ac = ACCEL_GET_CLASS(current_accel());
+    int ret;
 
     mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
 
@@ -520,6 +523,23 @@ void qemu_system_reset(ShutdownCause reason)
     default:
         type = RESET_TYPE_COLD;
     }
+
+    /*
+     * different accelerators implement how to close the old file handle of
+     * the accelerator descriptor and create a new one here. Resetting
+     * file handle is necessary to create a new confidential VM context post
+     * VM reset.
+     */
+    if (current_machine->cgs && reason == SHUTDOWN_CAUSE_GUEST_RESET) {
+        if (ac->reset_vmfd) {
+            ret = ac->reset_vmfd(current_machine);
+            if (ret < 0) {
+                error_report("unable to reset vmfd: %d", ret);
+                abort();
+            }
+        }
+    }
+
     if (mc && mc->reset) {
         mc->reset(current_machine, type);
     } else {
-- 
2.42.0
Re: [PATCH v2 02/32] hw/accel: add a per-accelerator callback to change VM accelerator handle
Posted by Paolo Bonzini 4 weeks ago
On Mon, Jan 12, 2026 at 2:23 PM Ani Sinha <anisinha@redhat.com> wrote:
> +    if (current_machine->cgs && reason == SHUTDOWN_CAUSE_GUEST_RESET) {

This should check (I think) !cpus_are_resettable(), instead of
current_machine->cgs.

> +        if (ac->reset_vmfd) {
> +            ret = ac->reset_vmfd(current_machine);
> +            if (ret < 0) {
> +                error_report("unable to reset vmfd: %d", ret);
> +                abort();

This should not be an abort, but it should change the runstate to
RUN_STATE_INTERNAL_ERROR.

Also, I would move the introduction of
confidential_guest_can_rebuild_state() callback even before this patch
(see upcoming review of patch 24).

At the end of the series, this should look something like:

    if (reason == SHUTDOWN_CAUSE_GUEST_RESET
        && (current_machine->new_accel_vmfd_on_reset
            || !cpus_are_resettable()) {
        if (ac->reset_vmfd) {
            ret = ac->reset_vmfd(current_machine);
            if (ret < 0) {
                 error_report(..., strerror(-ret));
                 vm_stop(RUN_STATE_INTERNAL_ERROR);
            }
        } else if (!cpus_are_resettable()) {
            error_report("accelerator does not support reset");
        } else {
            error_report("accelerator does not support
x-change-vmfd-on-reset=...,"
                        " proceeding with normal reset");
        }
    }

Paolo

> +            }
> +        }
> +    }
> +
>      if (mc && mc->reset) {
>          mc->reset(current_machine, type);
>      } else {
> --
> 2.42.0
>