Skip to contentSkip to conversation

s390x/pv: Add support for asynchronous teardown for reboot

PATCH Superseded Claudio Imbrenda 2 participants 6 messages Dec 22, 2022 – Jan 3, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover s390x/pv: Add support for asynchronous teardown for reboot

The first patch is just a minimal header update to compile the second patch; it can be safely discarded once the Linux headers are updated to 6.2.

The second patch adds support for asynchronous teardown of protected guests when rebooting. First the existing guest is prepared for asynchronous teardown, the rebooted guest will be able to continue immediately, while a background thread actually performs the necessary cleanup.

Claudio Imbrenda (2):
Linux header update
s390x/pv: Add support for asynchronous teardown for reboot

 hw/s390x/pv.c              | 28 ++++++++++++++++++++++++++++
 hw/s390x/s390-virtio-ccw.c |  5 ++++-
 include/hw/s390x/pv.h      |  2 ++
 linux-headers/linux/kvm.h  |  3 +++
 4 files changed, 37 insertions(+), 1 deletion(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/2 Linux header update

---
 linux-headers/linux/kvm.h | 3 +++
 1 file changed, 3 insertions(+)
linux-headers/linux/kvm.h +3 -0
index ebdafa576d..122b273433 100644
@@ -1175,6 +1175,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220
 #define KVM_CAP_S390_ZPCI_OP 221
 #define KVM_CAP_S390_CPU_TOPOLOGY 222
+#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1737,6 +1738,8 @@ enum pv_cmd_id {
 	KVM_PV_UNSHARE_ALL,
 	KVM_PV_INFO,
 	KVM_PV_DUMP,
+	KVM_PV_ASYNC_CLEANUP_PREPARE,
+	KVM_PV_ASYNC_CLEANUP_PERFORM,
 };
 
 struct kvm_pv_cmd {
Signature · 1 line
2.38.1
#2 lore raw

PATCH 2/2 s390x/pv: Add support for asynchronous teardown for reboot

In reply to Claudio Imbrenda

This patch adds support for the asynchronous teardown for reboot for protected VMs.

When attempting to tear down a protected VM, try to use the new asynchronous interface first. If that fails, fall back to the classic synchronous one.

The asynchronous interface involves invoking the new KVM_PV_ASYNC_DISABLE_PREPARE command for the KVM_S390_PV_COMMAND ioctl.

This will prepare the current protected VM for asynchronous teardown. Once the protected VM is prepared for teardown, execution can continue immediately.

Once the protected VM has been prepared, a new thread is started to actually perform the teardown. The new thread uses the new KVM_PV_ASYNC_DISABLE command for the KVM_S390_PV_COMMAND ioctl. The previously prepared protected VM is torn down in the new thread.

Once KVM_PV_ASYNC_DISABLE is invoked, it is possible to use KVM_PV_ASYNC_DISABLE_PREPARE again. If a protected VM has already been prepared and its cleanup has not started, it will not be possible to prepare a new VM. In that case the classic synchronous teardown has to be performed.

The synchronous teardown will now also clean up any prepared VMs whose asynchronous teardown has not been initiated yet.

This considerably speeds up the reboot of a protected VM; for large VMs especially, it could take a long time to perform a reboot with the traditional synchronous teardown, while with this patch it is almost immediate.

---
 hw/s390x/pv.c              | 28 ++++++++++++++++++++++++++++
 hw/s390x/s390-virtio-ccw.c |  5 ++++-
 include/hw/s390x/pv.h      |  2 ++
 3 files changed, 34 insertions(+), 1 deletion(-)
hw/s390x/pv.c +28 -0
index 8dfe92d8df..0b8345092b 100644
@@ -16,6 +16,7 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "sysemu/kvm.h"
+#include "sysemu/cpus.h"
 #include "qom/object_interfaces.h"
 #include "exec/confidential-guest-support.h"
 #include "hw/s390x/ipl.h"
@@ -107,6 +108,33 @@ void s390_pv_vm_disable(void)
      s390_pv_cmd_exit(KVM_PV_DISABLE, NULL);
 }
 
+static void *s390_pv_do_unprot_async_fn(void *p)
+{
+     s390_pv_cmd_exit(KVM_PV_ASYNC_CLEANUP_PERFORM, NULL);
+     return NULL;
+}
+
+bool s390_pv_vm_try_disable_async(void)
+{
+    char tname[VCPU_THREAD_NAME_SIZE];
+    QemuThread *t;
+
+    if (!kvm_check_extension(kvm_state, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE)) {
+        return false;
+    }
+    if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE, NULL) != 0) {
+        return false;
+    }
+
+    t = g_malloc0(sizeof(QemuThread));
+    snprintf(tname, VCPU_THREAD_NAME_SIZE, "async_unpr/KVM");
+
+    qemu_thread_create(t, tname, s390_pv_do_unprot_async_fn, NULL,
+                       QEMU_THREAD_DETACHED);
+
+    return true;
+}
+
 int s390_pv_set_sec_parms(uint64_t origin, uint64_t length)
 {
     struct kvm_s390_pv_sec_parm args = {
hw/s390x/s390-virtio-ccw.c +4 -1
index f22f61b8b6..503f212a31 100644
@@ -41,6 +41,7 @@
 #include "hw/qdev-properties.h"
 #include "hw/s390x/tod.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 #include "hw/s390x/pv.h"
 #include "migration/blocker.h"
 #include "qapi/visitor.h"
@@ -329,7 +330,9 @@ static inline void s390_do_cpu_ipl(CPUState *cs, run_on_cpu_data arg)
 
 static void s390_machine_unprotect(S390CcwMachineState *ms)
 {
-    s390_pv_vm_disable();
+    if (!s390_pv_vm_try_disable_async()) {
+        s390_pv_vm_disable();
+    }
     ms->pv = false;
     migrate_del_blocker(pv_mig_blocker);
     error_free_or_abort(&pv_mig_blocker);
include/hw/s390x/pv.h +2 -0
index 9360aa1091..966306a9db 100644
@@ -41,6 +41,7 @@ static inline bool s390_is_pv(void)
 int s390_pv_query_info(void);
 int s390_pv_vm_enable(void);
 void s390_pv_vm_disable(void);
+bool s390_pv_vm_try_disable_async(void);
 int s390_pv_set_sec_parms(uint64_t origin, uint64_t length);
 int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak);
 void s390_pv_prep_reset(void);
@@ -60,6 +61,7 @@ static inline bool s390_is_pv(void) { return false; }
 static inline int s390_pv_query_info(void) { return 0; }
 static inline int s390_pv_vm_enable(void) { return 0; }
 static inline void s390_pv_vm_disable(void) {}
+static inline bool s390_pv_vm_try_disable_async(void) { return false; }
 static inline int s390_pv_set_sec_parms(uint64_t origin, uint64_t length) { return 0; }
 static inline int s390_pv_unpack(uint64_t addr, uint64_t size, uint64_t tweak) { return 0; }
 static inline void s390_pv_prep_reset(void) {}
Signature · 1 line
2.38.1
#3 lore raw
Quoted 2 lines · Claudio Imbrenda

This patch adds support for the asynchronous teardown for reboot for protected VMs.

[...]

Quoted 7 lines
hw/s390x/pv.c +7 -0
+bool s390_pv_vm_try_disable_async(void)
+{
+    char tname[VCPU_THREAD_NAME_SIZE];
+    QemuThread *t;
+
+    if (!kvm_check_extension(kvm_state, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE)) {
+        return false;

hw/s390x/pv.c void s390_pv_vm_disable(void) · line 133

Quoted 10 lines
hw/s390x/pv.c +10 -0
+    }
+    if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE, NULL) != 0) {
+        return false;
+    }
+
+    t = g_malloc0(sizeof(QemuThread));
+    snprintf(tname, VCPU_THREAD_NAME_SIZE, "async_unpr/KVM");
+
+    qemu_thread_create(t, tname, s390_pv_do_unprot_async_fn, NULL,
+                       QEMU_THREAD_DETACHED);

I think you could get along without the tname[] array here by simply passing the string directly to qemu_thread_create() ?

Apart from that, patch looks fine to me.

Thomas

#4 lore raw
Quoted 16 lines · On Tue, 3 Jan 2023 12:27:32 +0100 Thomas Huth <[email protected]> wrote:
Quoted 2 lines · Claudio Imbrenda

This patch adds support for the asynchronous teardown for reboot for protected VMs.

[...]

Quoted 12 lines
hw/s390x/pv.c +12 -0
+bool s390_pv_vm_try_disable_async(void)
+{
+    char tname[VCPU_THREAD_NAME_SIZE];
+    QemuThread *t;
+
+    if (!kvm_check_extension(kvm_state, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE)) {
+        return false;
+    }
+    if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE, NULL) != 0) {
+        return false;
+    }
+
Quoted 8 lines

hw/s390x/pv.c void s390_pv_vm_disable(void) · line 133

Quoted 5 lines
hw/s390x/pv.c +5 -0
+    t = g_malloc0(sizeof(QemuThread));
+    snprintf(tname, VCPU_THREAD_NAME_SIZE, "async_unpr/KVM");
+
+    qemu_thread_create(t, tname, s390_pv_do_unprot_async_fn, NULL,
+                       QEMU_THREAD_DETACHED);  

I think you could get along without the tname[] array here by simply passing the string directly to qemu_thread_create() ?

ahh, you're right

maybe I should also pick a better name? in retrospect it looks quite cryptic

Quoted 5 lines

Apart from that, patch looks fine to me.

Thomas

#5 lore raw
Quoted 28 lines · Claudio Imbrenda
Quoted 16 lines · On Tue, 3 Jan 2023 12:27:32 +0100 Thomas Huth <[email protected]> wrote:
Quoted 2 lines · Claudio Imbrenda

This patch adds support for the asynchronous teardown for reboot for protected VMs.

[...]

Quoted 12 lines
hw/s390x/pv.c +12 -0
+bool s390_pv_vm_try_disable_async(void)
+{
+    char tname[VCPU_THREAD_NAME_SIZE];
+    QemuThread *t;
+
+    if (!kvm_check_extension(kvm_state, KVM_CAP_S390_PROTECTED_ASYNC_DISABLE)) {
+        return false;
+    }
+    if (s390_pv_cmd(KVM_PV_ASYNC_CLEANUP_PREPARE, NULL) != 0) {
+        return false;
+    }
+
Quoted 8 lines

hw/s390x/pv.c void s390_pv_vm_disable(void) · line 133

Quoted 5 lines
hw/s390x/pv.c +5 -0
+    t = g_malloc0(sizeof(QemuThread));
+    snprintf(tname, VCPU_THREAD_NAME_SIZE, "async_unpr/KVM");
+
+    qemu_thread_create(t, tname, s390_pv_do_unprot_async_fn, NULL,
+                       QEMU_THREAD_DETACHED);

I think you could get along without the tname[] array here by simply passing the string directly to qemu_thread_create() ?

Quoted 4 lines

ahh, you're right

maybe I should also pick a better name? in retrospect it looks quite cryptic

Yes, please!

Thomas