Skip to contentSkip to conversation

tpm_emulator: Avoid double initialization during migration

PATCH In review Ross Lagerwall via 4 participants 4 messages Aug 1, 2022 – Aug 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm_emulator: Avoid double initialization during migration

When resuming after a migration, the backend sends CMD_INIT to the emulator from the startup callback, then it sends the migration state from the vmstate to the emulator, then it sends CMD_INIT again. Skip the first CMD_INIT during a migration to avoid initializing the TPM twice.

---
 backends/tpm/tpm_emulator.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
backends/tpm/tpm_emulator.c +10 -0
index 87d061e9bb..9b50c5b3e2 100644
@@ -32,6 +32,7 @@
 #include "qemu/sockets.h"
 #include "qemu/lockable.h"
 #include "io/channel-socket.h"
+#include "sysemu/runstate.h"
 #include "sysemu/tpm_backend.h"
 #include "sysemu/tpm_util.h"
 #include "tpm_int.h"
@@ -383,6 +384,15 @@ err_exit:
 
 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
 {
+    /* TPM startup will be done from post_load hook */
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        if (buffersize != 0) {
+            return tpm_emulator_set_buffer_size(tb, buffersize, NULL);
+        }
+
+        return 0;
+    }
+
     return tpm_emulator_startup_tpm_resume(tb, buffersize, false);
 }
 
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 7 lines · Ross Lagerwall via

When resuming after a migration, the backend sends CMD_INIT to the emulator from the startup callback, then it sends the migration state from the vmstate to the emulator, then it sends CMD_INIT again. Skip the first CMD_INIT during a migration to avoid initializing the TPM twice.

lgtm

There are no visible bugs/symptoms, I suppose?

Quoted 37 lines
---
 backends/tpm/tpm_emulator.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
backends/tpm/tpm_emulator.c +10 -1
index 87d061e9bb..9b50c5b3e2 100644
@@ -32,6 +32,7 @@
 #include "qemu/sockets.h"
 #include "qemu/lockable.h"
 #include "io/channel-socket.h"
+#include "sysemu/runstate.h"
 #include "sysemu/tpm_backend.h"
 #include "sysemu/tpm_util.h"
 #include "tpm_int.h"
@@ -383,6 +384,15 @@ err_exit:
 
 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
 {
+    /* TPM startup will be done from post_load hook */
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        if (buffersize != 0) {
+            return tpm_emulator_set_buffer_size(tb, buffersize, NULL);
+        }
+
+        return 0;
+    }
+
     return tpm_emulator_startup_tpm_resume(tb, buffersize, false);
 }
 
--

2.31.1

Signature · 1 line
Marc-André Lureau
#2 lore raw
Quoted 15 lines

From: Marc-André Lureau <[email protected]>
Sent: Monday, August 1, 2022 3:36 PM
To: Ross Lagerwall <[email protected]>
Cc: Stefan Berger <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] tpm_emulator: Avoid double initialization during migration

[CAUTION - EXTERNAL EMAIL] DO NOT reply, click links, or open attachments unless you have verified the sender and know the content is safe. Hi

On Mon, Aug 1, 2022 at 6:28 PM Ross Lagerwall via <[email protected]> wrote:
When resuming after a migration, the backend sends CMD_INIT to the emulator from the startup callback, then it sends the migration state from the vmstate to the emulator, then it sends CMD_INIT again. Skip the first CMD_INIT during a migration to avoid initializing the TPM twice.

Quoted 6 lines

lgtm

There are no visible bugs/symptoms, I suppose?

I started looking into this because swtpm would complain about "tpm2-00.volatilestate" being missing during migration. This happened during the first init because the volatile state only got set by QEMU before the 2nd init. I'm not sure if there are any other negative consequences to sending init twice (I suspect probably not?).

Ross

Tested-by on v1 1/1
#3 lore raw
In reply to Ross Lagerwall via
Quoted 2 lines · Ross Lagerwall via

When resuming after a migration, the backend sends CMD_INIT to the emulator from the startup callback, then it sends the migration state

This startup hook is called upon TIS/CRB device reset, so this is likely called before the device state has been received.

Quoted 2 lines

from the vmstate to the emulator, then it sends CMD_INIT again. Skip the first CMD_INIT during a migration to avoid initializing the TPM twice.

Ok, that's sufficient to start it up once all the state has been received.

Quoted 2 lines
Quoted 32 lines
---
  backends/tpm/tpm_emulator.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
backends/tpm/tpm_emulator.c +10 -0
index 87d061e9bb..9b50c5b3e2 100644
@@ -32,6 +32,7 @@
  #include "qemu/sockets.h"
  #include "qemu/lockable.h"
  #include "io/channel-socket.h"
+#include "sysemu/runstate.h"
  #include "sysemu/tpm_backend.h"
  #include "sysemu/tpm_util.h"
  #include "tpm_int.h"
@@ -383,6 +384,15 @@ err_exit:
 
  static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
  {
+    /* TPM startup will be done from post_load hook */
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        if (buffersize != 0) {
+            return tpm_emulator_set_buffer_size(tb, buffersize, NULL);
+        }
+
+        return 0;
+    }
+
      return tpm_emulator_startup_tpm_resume(tb, buffersize, false);
  }