Skip to contentSkip to conversation

tpm: a few fixes

PATCH In review Stefan Berger 3 participants 15 messages Nov 14, 2017 – Dec 20, 2017

lore
Share & tools
lore
#0 lore raw

v3 cover tpm: a few fixes

From: Stefan Berger <Stefan Berger [email protected]>

The following patches fix a performance issue (patch 1) and an error path issue (patches 2 and 3) for 2.11.

Stefan

Stefan Berger (3):
tpm_emulator: Add a caching layer for the TPM Established flag
tpm_tis: Return TPM_VERSION_UNSPEC in case of BE failure
tpm_tis: Return 0 for every register in case of failure mode

 hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
 hw/tpm/tpm_tis.c      |  6 +++++-
 2 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH v3 1/3 tpm_emulator: Add a caching layer for the TPM Established flag

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

v1->v2:
- move the caching to the backend layer since detecting the TPM 1.2 TSC_ResetEstablishmentBit() command is easier to do here.

---
 hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
hw/tpm/tpm_emulator.c +14 -3
index e1a6810..b293db7 100644
@@ -73,6 +73,9 @@ typedef struct TPMEmulator {
     Error *migration_blocker;
 
     QemuMutex mutex;
+
+    unsigned int established_flag:1;
+    unsigned int established_flag_cached:1;
 } TPMEmulator;
 
 
@@ -287,16 +290,22 @@ static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
     TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
     ptm_est est;
 
-    DPRINTF("%s", __func__);
+    if (tpm_emu->established_flag_cached) {
+        return tpm_emu->established_flag;
+    }
+
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
                              0, sizeof(est)) < 0) {
         error_report("tpm-emulator: Could not get the TPM established flag: %s",
                      strerror(errno));
         return false;
     }
-    DPRINTF("established flag: %0x", est.u.resp.bit);
+    DPRINTF("got established flag: %0x", est.u.resp.bit);
+
+    tpm_emu->established_flag_cached = 1;
+    tpm_emu->established_flag = (est.u.resp.bit != 0);
 
-    return (est.u.resp.bit != 0);
+    return tpm_emu->established_flag;
 }
 
 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
@@ -327,6 +336,8 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
         return -1;
     }
 
+    tpm_emu->established_flag_cached = 0;
+
     return 0;
 }
 
Signature · 1 line
2.5.5
#9 lore raw

PATCH v3 2/3 tpm_tis: Return TPM_VERSION_UNSPEC in case of BE failure

In reply to Stefan Berger

In case the backend has a failure, such as the tpm_emulator's CMD_INIT failing, the TIS goes into failure mode and does not respond to reads or writes to MMIO registers. In this case we need to prevent the ACPI table from being added and the straight-forward way is to indicate that there's no known TPM version being used.

---
 hw/tpm/tpm_tis.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/tpm/tpm_tis.c +4 -0
index 7402528..fec2fc6 100644
@@ -1008,6 +1008,10 @@ TPMVersion tpm_tis_get_tpm_version(Object *obj)
 {
     TPMState *s = TPM(obj);
 
+    if (tpm_backend_had_startup_error(s->be_driver)) {
+        return TPM_VERSION_UNSPEC;
+    }
+
     return tpm_backend_get_tpm_version(s->be_driver);
 }
 
Signature · 1 line
2.5.5
#11 lore raw

PATCH v3 3/3 tpm_tis: Return 0 for every register in case of failure mode

In reply to Stefan Berger

Rather than returning ~0, return 0 for every register in case of failure mode. The '0' is better to indicate that there's no device there.

---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index fec2fc6..42d647d 100644
@@ -545,7 +545,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
     uint8_t v;
 
     if (tpm_backend_had_startup_error(s->be_driver)) {
-        return val;
+        return 0;
     }
 
     switch (offset) {
Signature · 1 line
2.5.5
#2 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Stefan Berger

Hi

Quoted 3 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

What's the impact? Isn't this just a "small" optimization? Iotw, why is this for-2.11?

Quoted 62 lines

v1->v2:
- move the caching to the backend layer since detecting the TPM 1.2 TSC_ResetEstablishmentBit() command is easier to do here.

---
 hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
hw/tpm/tpm_emulator.c +14 -4
index e1a6810..b293db7 100644
@@ -73,6 +73,9 @@ typedef struct TPMEmulator {
     Error *migration_blocker;
 
     QemuMutex mutex;
+
+    unsigned int established_flag:1;
+    unsigned int established_flag_cached:1;
 } TPMEmulator;
 
 
@@ -287,16 +290,22 @@ static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
     TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
     ptm_est est;
 
-    DPRINTF("%s", __func__);
+    if (tpm_emu->established_flag_cached) {
+        return tpm_emu->established_flag;
+    }
+
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
                              0, sizeof(est)) < 0) {
         error_report("tpm-emulator: Could not get the TPM established flag: %s",
                      strerror(errno));
         return false;
     }
-    DPRINTF("established flag: %0x", est.u.resp.bit);
+    DPRINTF("got established flag: %0x", est.u.resp.bit);
+
+    tpm_emu->established_flag_cached = 1;
+    tpm_emu->established_flag = (est.u.resp.bit != 0);
 
-    return (est.u.resp.bit != 0);
+    return tpm_emu->established_flag;
 }
 
 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
@@ -327,6 +336,8 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
         return -1;
     }
 
+    tpm_emu->established_flag_cached = 0;
+
     return 0;
 }
 
--

2.5.5

Signature · 1 line
Marc-André Lureau
Reviewed-by on v3 2/3
#10 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 2/3] tpm_tis: Return TPM_VERSION_UNSPEC in case of BE failure

In reply to Stefan Berger

Hi

Quoted 7 lines · Stefan Berger

In case the backend has a failure, such as the tpm_emulator's CMD_INIT failing, the TIS goes into failure mode and does not respond to reads or writes to MMIO registers. In this case we need to prevent the ACPI table from being added and the straight-forward way is to indicate that there's no known TPM version being used.

(we can probably iterate to improve the code around that later)

Quoted 22 lines
---
 hw/tpm/tpm_tis.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/tpm/tpm_tis.c +4 -1
index 7402528..fec2fc6 100644
@@ -1008,6 +1008,10 @@ TPMVersion tpm_tis_get_tpm_version(Object *obj)
 {
     TPMState *s = TPM(obj);
 
+    if (tpm_backend_had_startup_error(s->be_driver)) {
+        return TPM_VERSION_UNSPEC;
+    }
+
     return tpm_backend_get_tpm_version(s->be_driver);
 }
 
--

2.5.5

Signature · 1 line
Marc-André Lureau
#12 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 3/3] tpm_tis: Return 0 for every register in case of failure mode

In reply to Stefan Berger

Hi

Quoted 3 lines · Stefan Berger

Rather than returning ~0, return 0 for every register in case of failure mode. The '0' is better to indicate that there's no device there.

For most registers, 0 makes more sense. However, I wonder if we shouldn't just fail to start qemu in this case...

Not convincing me this is 2.11 material either. Does this fix a specific bug?

Quoted 21 lines
---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -2
index fec2fc6..42d647d 100644
@@ -545,7 +545,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
     uint8_t v;
 
     if (tpm_backend_had_startup_error(s->be_driver)) {
-        return val;
+        return 0;
     }
 
     switch (offset) {
--

2.5.5

Signature · 1 line
Marc-André Lureau
#3 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Marc-André Lureau
Quoted 9 lines · Marc-André Lureau

Hi

Quoted 3 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

What's the impact? Isn't this just a "small" optimization? Iotw, why is this for-2.11?

The TIS has a register that contains this flag and that's being polled quite frequently. So it generates a lot of traffic to the emulator. This caching layer gets rid of most of the traffic.

Stefan

Quoted 65 lines
Quoted 63 lines

v1->v2:
- move the caching to the backend layer since detecting the TPM 1.2 TSC_ResetEstablishmentBit() command is easier to do here.

---
  hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
hw/tpm/tpm_emulator.c +14 -4
index e1a6810..b293db7 100644
@@ -73,6 +73,9 @@ typedef struct TPMEmulator {
      Error *migration_blocker;
 
      QemuMutex mutex;
+
+    unsigned int established_flag:1;
+    unsigned int established_flag_cached:1;
  } TPMEmulator;
 
 
@@ -287,16 +290,22 @@ static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
      TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
      ptm_est est;
 
-    DPRINTF("%s", __func__);
+    if (tpm_emu->established_flag_cached) {
+        return tpm_emu->established_flag;
+    }
+
      if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
                               0, sizeof(est)) < 0) {
          error_report("tpm-emulator: Could not get the TPM established flag: %s",
                       strerror(errno));
          return false;
      }
-    DPRINTF("established flag: %0x", est.u.resp.bit);
+    DPRINTF("got established flag: %0x", est.u.resp.bit);
+
+    tpm_emu->established_flag_cached = 1;
+    tpm_emu->established_flag = (est.u.resp.bit != 0);
 
-    return (est.u.resp.bit != 0);
+    return tpm_emu->established_flag;
  }
 
  static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
@@ -327,6 +336,8 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
          return -1;
      }
 
+    tpm_emu->established_flag_cached = 0;
+
      return 0;
  }
 
--

2.5.5

#13 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 3/3] tpm_tis: Return 0 for every register in case of failure mode

In reply to Marc-André Lureau
Quoted 11 lines · Marc-André Lureau

Hi

Quoted 3 lines · Stefan Berger

Rather than returning ~0, return 0 for every register in case of failure mode. The '0' is better to indicate that there's no device there.

For most registers, 0 makes more sense. However, I wonder if we shouldn't just fail to start qemu in this case...

Not convincing me this is 2.11 material either. Does this fix a specific bug?

Yes, SeaBIOS detects the ~0 when it probes and thinks there's a device there. It then hangs trying to set flags and read registers to be able to use the device.

Stefan

Quoted 24 lines
Quoted 22 lines
---
  hw/tpm/tpm_tis.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -2
index fec2fc6..42d647d 100644
@@ -545,7 +545,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
      uint8_t v;
 
      if (tpm_backend_had_startup_error(s->be_driver)) {
-        return val;
+        return 0;
      }
 
      switch (offset) {
--

2.5.5

Reviewed-by
#14 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 3/3] tpm_tis: Return 0 for every register in case of failure mode

Hi

Quoted 14 lines · Stefan Berger
Quoted 13 lines · Marc-André Lureau

Hi

Quoted 4 lines · Stefan Berger

Rather than returning ~0, return 0 for every register in case of failure mode. The '0' is better to indicate that there's no device there.

For most registers, 0 makes more sense. However, I wonder if we shouldn't just fail to start qemu in this case...

Quoted 8 lines
Quoted 2 lines

Not convincing me this is 2.11 material either. Does this fix a specific bug?

Yes, SeaBIOS detects the ~0 when it probes and thinks there's a device there. It then hangs trying to set flags and read registers to be able to use the device.

Please update the commit message with that added,

Quoted 29 lines

Stefan

Quoted 25 lines
Quoted 23 lines
---
  hw/tpm/tpm_tis.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index fec2fc6..42d647d 100644
@@ -545,7 +545,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque,
 hwaddr addr,
      uint8_t v;
 
      if (tpm_backend_had_startup_error(s->be_driver)) {
-        return val;
+        return 0;
      }
 
      switch (offset) {

--
2.5.5

Signature · 1 line
Marc-André Lureau
#4 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Stefan Berger

Hi

Quoted 11 lines · Stefan Berger
Quoted 10 lines · Marc-André Lureau

Hi

Quoted 4 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

Quoted 7 lines
Quoted 2 lines

What's the impact? Isn't this just a "small" optimization? Iotw, why is this for-2.11?

The TIS has a register that contains this flag and that's being polled quite frequently. So it generates a lot of traffic to the emulator. This caching layer gets rid of most of the traffic.

I didn't notice any problem when doing my tests, I guess Amarnath niether. perhaps it's best to delay for after 2.11.

Quoted 72 lines

Stefan

Quoted 68 lines
Quoted 66 lines

v1->v2:
- move the caching to the backend layer since detecting the TPM 1.2 TSC_ResetEstablishmentBit() command is easier to do here.

---
  hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
hw/tpm/tpm_emulator.c +12 -3
index e1a6810..b293db7 100644
@@ -73,6 +73,9 @@ typedef struct TPMEmulator {
      Error *migration_blocker;
 
      QemuMutex mutex;
+
+    unsigned int established_flag:1;
+    unsigned int established_flag_cached:1;
  } TPMEmulator;
 
 
@@ -287,16 +290,22 @@ static bool
 tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
      TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
      ptm_est est;
 
-    DPRINTF("%s", __func__);
+    if (tpm_emu->established_flag_cached) {
+        return tpm_emu->established_flag;
+    }
+
      if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
                               0, sizeof(est)) < 0) {
          error_report("tpm-emulator: Could not get the TPM established
 flag: %s",
                       strerror(errno));
          return false;
      }
-    DPRINTF("established flag: %0x", est.u.resp.bit);
+    DPRINTF("got established flag: %0x", est.u.resp.bit);
+
+    tpm_emu->established_flag_cached = 1;
+    tpm_emu->established_flag = (est.u.resp.bit != 0);
 
-    return (est.u.resp.bit != 0);
+    return tpm_emu->established_flag;
  }

static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,

hw/tpm/tpm_emulator.c +2 -1
@@ -327,6 +336,8 @@ static int
 tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
          return -1;
      }
 
+    tpm_emu->established_flag_cached = 0;
+
      return 0;
  }
 
--

2.5.5

Signature · 1 line
Marc-André Lureau
#5 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

Quoted 23 lines · Marc-André Lureau

Hi

Quoted 19 lines · Stefan Berger
Quoted 16 lines · Marc-André Lureau

Hi

Quoted 7 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't
need to go to the emulator every time the flag is read by accessing
the REG_ACCESS register.

What's the impact? Isn't this just a "small" optimization? Iotw, why
is this for-2.11?

Quoted 7 lines
Quoted 5 lines

The TIS has a register that contains this flag and that's being polled quite
frequently. So it generates a lot of traffic to the emulator. This caching
layer gets rid of most of the traffic.

I didn't notice any problem when doing my tests, I guess Amarnath niether. perhaps it's best to delay for after 2.11.

Yes, I could see there is little frequent(21 calls to backend) to get establish flag, and this patch will help to avoid them. But i still agree with Marc and tag this as "small" optimization.

- Amarnath

#7 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Marc-André Lureau
Quoted 12 lines · Marc-André Lureau

Hi

Quoted 8 lines · Stefan Berger
Quoted 7 lines · Marc-André Lureau

Hi

Quoted 3 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

What's the impact? Isn't this just a "small" optimization? Iotw, why is this for-2.11?

The TIS has a register that contains this flag and that's being polled quite frequently. So it generates a lot of traffic to the emulator. This caching layer gets rid of most of the traffic.

I didn't notice any problem when doing my tests, I guess Amarnath niether. perhaps it's best to delay for after 2.11.

So then let's delay it. Following a Reviewed-by I'd put it in the tpm-next queue then.

Stefan

#6 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Valluri, Amarnath
Quoted 25 lines · Amarnath
Quoted 24 lines · Marc-André Lureau

Hi

Quoted 20 lines · Stefan Berger
Quoted 6 lines · Marc-André Lureau

Hi

Quoted 1 line · Stefan Berger
Quoted 8 lines
Quoted 5 lines

Add a caching layer for the TPM established flag so that we don't
need to go to the emulator every time the flag is read by accessing
the REG_ACCESS register.

What's the impact? Isn't this just a "small" optimization? Iotw, why
is this for-2.11?

The TIS has a register that contains this flag and that's being polled quite
frequently. So it generates a lot of traffic to the emulator. This caching
layer gets rid of most of the traffic.

Quoted 5 lines
Quoted 2 lines

I didn't notice any problem when doing my tests, I guess Amarnath niether. perhaps it's best to delay for after 2.11.

Yes, I could see there is little frequent(21 calls to backend) to get establish flag, and this patch will help to avoid them. But i still agree with Marc and tag this as "small" optimization.

Can you review it so we have it ready for 2.12?

Thanks,
Stefan

Quoted 2 lines

- Amarnath

Reviewed-by on v3 1/3
#8 lore raw

Re: [Qemu-devel] [PATCH v3 for-2.11 1/3] tpm_emulator: Add a caching layer for the TPM Established flag

In reply to Stefan Berger

Hi

Quoted 5 lines · Stefan Berger

Add a caching layer for the TPM established flag so that we don't need to go to the emulator every time the flag is read by accessing the REG_ACCESS register.

looks good,

Quoted 61 lines

v1->v2:
- move the caching to the backend layer since detecting the TPM 1.2 TSC_ResetEstablishmentBit() command is easier to do here.

---
 hw/tpm/tpm_emulator.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
hw/tpm/tpm_emulator.c +14 -4
index e1a6810..b293db7 100644
@@ -73,6 +73,9 @@ typedef struct TPMEmulator {
     Error *migration_blocker;
 
     QemuMutex mutex;
+
+    unsigned int established_flag:1;
+    unsigned int established_flag_cached:1;
 } TPMEmulator;
 
 
@@ -287,16 +290,22 @@ static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
     TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
     ptm_est est;
 
-    DPRINTF("%s", __func__);
+    if (tpm_emu->established_flag_cached) {
+        return tpm_emu->established_flag;
+    }
+
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
                              0, sizeof(est)) < 0) {
         error_report("tpm-emulator: Could not get the TPM established flag: %s",
                      strerror(errno));
         return false;
     }
-    DPRINTF("established flag: %0x", est.u.resp.bit);
+    DPRINTF("got established flag: %0x", est.u.resp.bit);
+
+    tpm_emu->established_flag_cached = 1;
+    tpm_emu->established_flag = (est.u.resp.bit != 0);
 
-    return (est.u.resp.bit != 0);
+    return tpm_emu->established_flag;
 }
 
 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
@@ -327,6 +336,8 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
         return -1;
     }
 
+    tpm_emu->established_flag_cached = 0;
+
     return 0;
 }
 
--

2.5.5

Signature · 1 line
Marc-André Lureau