Skip to contentSkip to conversation

tpm: Handle transient NitroTPM unavailability

PATCH Superseded Surendran Kanagaraj 3 participants 12 messages Sep 23 – Sep 30

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tpm: Handle transient NitroTPM unavailability

TPM commands need to complete within the duration defined in the TPM2 spec or keep answering TPM2_RC_RETRY for at most TPM2_DURATION_LONG (2s).

Devices that have different timeout requirements than the TPM2 spec can either exhaust the retry budget or delay the command completion. NitroTPM can very rarely enter a mode where it either sends TPM2_RC_RETRY for more than 2s or completes the command late.

When this happens in tpm2_load_null() during auth session start, the driver sets TPM_CHIP_FLAG_DISABLE and fails all subsequent TPM requests.

tpm tpm0: in retry loop
tpm tpm0: tpm2_load_context: failed with a TPM error 0x0922

Add chip->busy_timeout_ms to support devices that have this unavailability window (patch 1). It raises the TPM2_RC_RETRY budget and the per command duration to at least that value. The CRB driver reads the vendor ID from the interface ID register and sets 30s for NitroTPM (patch 2)

Tested on an EC2 instance with NitroTPM by inducing unavailability windows under HMAC session workloads, and in QEMU with swtpm by holding the TPM in TPM2_RC_RETRY and stalling command completion past 2s.

Surendran Kanagaraj (2):
tpm: Add per-chip timeout for transient unavailability
tpm_crb: Raise timeouts for Amazon NitroTPM

 drivers/char/tpm/tpm-interface.c | 11 +++++--
 drivers/char/tpm/tpm.h           |  2 +-
 drivers/char/tpm/tpm2-cmd.c      | 17 +++++++---
 drivers/char/tpm/tpm_crb.c       | 54 ++++++++++++++++++++++++++++++++
 include/linux/tpm.h              |  7 +++++
 5 files changed, 82 insertions(+), 9 deletions(-)

base-commit: ab411db3c3b1fd0c70a36fb32c96b2c60175210b

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 tpm: Add per-chip timeout for transient unavailability

TPM commands need to complete within the command duration defined in the TPM2 spec or to keep answering TPM2_RC_RETRY for at most TPM2_DURATION_LONG (2s).

Devices that have different timeout requirements than the TPM2 spec could exhaust the retry budget or exceed the command duration. These failures disable the device during an auth session, failing all subsequent TPM requests. Worse, when a TPM2_CC_FLUSH_CONTEXT command fails, it leaks the TPM's transient memory:

tpm tpm0: in retry loop
tpm tpm0: tpm2_load_context: failed with a TPM error 0x0922
...
tpm tpm0: A TPM error (2338) occurred flushing context

Fix this by adding chip->busy_timeout_ms to support devices that know the expected delay window. This value raises the TPM2_RC_RETRY retry budget and per-command durations to at least busy_timeout_ms. Chips that leave it at 0 keep the current timeouts. The driver sets it for NitroTPM in the following patch.

Tested with CONFIG_TCG_TPM2_HMAC=y and the following patch with the NitroTPM quirk applied, in QEMU with swtpm by stalling commands and holding the TPM in TPM2_RC_RETRY.

Assisted-by LLM
---
 drivers/char/tpm/tpm-interface.c | 11 ++++++++---
 drivers/char/tpm/tpm.h           |  2 +-
 drivers/char/tpm/tpm2-cmd.c      | 17 ++++++++++++-----
 include/linux/tpm.h              |  7 +++++++
 4 files changed, 28 insertions(+), 9 deletions(-)
drivers/char/tpm/tpm-interface.c +8 -3
index 0bab78c8767c..a423395b201a 100644
@@ -53,7 +53,7 @@ MODULE_PARM_DESC(suspend_pcr,
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 {
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
-		return tpm2_calc_ordinal_duration(ordinal);
+		return tpm2_calc_ordinal_duration(chip, ordinal);
 	else
 		return tpm1_calc_ordinal_duration(chip, ordinal);
 }
@@ -213,7 +213,8 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
  *
  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
  * the TPM and retransmits the command after a delay up to a maximum wait of
- * TPM2_DURATION_LONG.
+ * TPM2_DURATION_LONG, or chip->busy_timeout_ms when the driver declared a
+ * longer transient unavailability window.
  *
  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
  * only.
@@ -228,6 +229,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 	/* space for header and handles */
 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
 	unsigned int delay_msec = TPM2_DURATION_SHORT;
+	unsigned int max_delay_msec = TPM2_DURATION_LONG;
 	u32 rc = 0;
 	ssize_t ret;
 	const size_t save_size = min(sizeof(save), bufsiz);
@@ -241,6 +243,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 	 */
 	memcpy(save, buf, save_size);
 
+	if (chip->busy_timeout_ms > max_delay_msec)
+		max_delay_msec = chip->busy_timeout_ms;
+
 	for (;;) {
 		ret = tpm_try_transmit(chip, buf, bufsiz);
 		if (ret < 0)
@@ -255,7 +260,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
 			break;
 
-		if (delay_msec > TPM2_DURATION_LONG) {
+		if (delay_msec > max_delay_msec) {
 			if (rc == TPM2_RC_RETRY)
 				dev_err(&chip->dev, "in retry loop\n");
 			else
drivers/char/tpm/tpm.h +1 -1
index fa554c5ad80b..457eba8d03dd 100644
@@ -119,7 +119,7 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
 ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
 int tpm2_auto_startup(struct tpm_chip *chip);
 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
-unsigned long tpm2_calc_ordinal_duration(u32 ordinal);
+unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm2_probe(struct tpm_chip *chip);
 int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
 int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
drivers/char/tpm/tpm2-cmd.c +12 -5
index ae22295df798..dc5ed57fefe1 100644
@@ -78,20 +78,27 @@ static const struct {
 
 /**
  * tpm2_calc_ordinal_duration() - Calculate the maximum command duration
+ * @chip:	TPM chip to use.
  * @ordinal: TPM command ordinal.
  *
  * Returns the maximum amount of time the chip is expected by kernel to
- * take in jiffies.
+ * take in jiffies. The duration is never lower than chip->busy_timeout_ms.
  */
-unsigned long tpm2_calc_ordinal_duration(u32 ordinal)
+unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 {
+	unsigned long duration = TPM2_DURATION_DEFAULT;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(tpm2_ordinal_duration_map); i++)
-		if (ordinal == tpm2_ordinal_duration_map[i].ordinal)
-			return msecs_to_jiffies(tpm2_ordinal_duration_map[i].duration);
+		if (ordinal == tpm2_ordinal_duration_map[i].ordinal) {
+			duration = tpm2_ordinal_duration_map[i].duration;
+			break;
+		}
+
+	if (duration < chip->busy_timeout_ms)
+		duration = chip->busy_timeout_ms;
 
-	return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
+	return msecs_to_jiffies(duration);
 }
 
 /**
include/linux/tpm.h +7 -0
index 0db277af45c3..7089067412d3 100644
@@ -140,6 +140,13 @@ struct tpm_chip {
 	unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
 	bool duration_adjusted;
 
+	/*
+	 * Longest unavailability expected from the chip in ms. Raises the
+	 * TPM2_RC_RETRY retry budget and the per-command durations to at
+	 * least this value; 0 keeps the defaults.
+	 */
+	unsigned int busy_timeout_ms;
+
 	struct dentry *bios_dir;
 
 	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
Signature · 1 line
2.47.3
#7 lore raw

PATCH 2/2 tpm_crb: Raise timeouts for Amazon NitroTPM

In reply to Surendran Kanagaraj

TPM commands need to complete within the command duration defined in the TPM2 spec or keep answering TPM2_RC_RETRY for at most TPM2_DURATION_LONG (2s).

In rare scenarios NitroTPM can enter a mode where it either sends TPM2_RC_RETRY for more than 2s or delays command completion. When this happens during auth session start, the driver disables the chip and every subsequent request fails:

tpm tpm0: in retry loop
tpm tpm0: tpm2_load_context: failed with a TPM error 0x0922

Add support for vendor quirks to the CRB driver. The table is keyed by the vendor ID read from the interface ID register. For NitroTPM, set busy_timeout_ms to 30s, which covers the longest unavailability time along with some headroom.

Tested on an EC2 instance with NitroTPM by inducing multiple unavailability windows with no TPM errors, and in QEMU with swtpm by holding the TPM in TPM2_RC_RETRY and stalling command completion for longer than 2s with the quirk applied.

Assisted-by LLM
---
 drivers/char/tpm/tpm_crb.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/char/tpm/tpm_crb.c +54 -0
index ceb4100ba400..4107203beec9 100644
@@ -13,6 +13,7 @@
 
 #include <linux/acpi.h>
 #include <linux/highmem.h>
+#include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/rculist.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -26,6 +27,15 @@
 #define ACPI_SIG_TPM2 "TPM2"
 #define TPM_CRB_MAX_RESOURCES 3
 
+/* TPM_CRB_INTF_ID_x vendor ID field, bits 47:32 of the 64-bit register */
+#define CRB_INTF_ID_VID(intf_id)	(((intf_id) >> 32) & 0xffff)
+
+/* Amazon NitroTPM */
+#define CRB_INTF_VID_AMZN		0x0ec2
+
+/* Longest unavailability seen from NitroTPM */
+#define CRB_AMZN_BUSY_TIMEOUT_MS	30000
+
 static const guid_t crb_acpi_start_guid =
 	GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
 		  0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
@@ -783,6 +793,48 @@ static int crb_map_pluton(struct device *dev, struct crb_priv *priv,
 	return 0;
 }
 
+/*
+ * Read the vendor ID from the interface ID register. Returns -ENODEV when
+ * the head registers are not mapped for this start method.
+ */
+static int crb_vendor_id(struct crb_priv *priv)
+{
+	u64 intf_id;
+
+	if (!priv->regs_h)
+		return -ENODEV;
+
+	intf_id = lo_hi_readq(&priv->regs_h->intf_id);
+
+	return CRB_INTF_ID_VID(intf_id);
+}
+
+static void crb_amzn_quirk(struct tpm_chip *chip)
+{
+	/* NitroTPM requires larger timeouts */
+	chip->busy_timeout_ms = CRB_AMZN_BUSY_TIMEOUT_MS;
+}
+
+static const struct crb_vendor_quirk {
+	u16 vendor_id;
+	void (*apply)(struct tpm_chip *chip);
+} crb_vendor_quirks[] = {
+	{ CRB_INTF_VID_AMZN, crb_amzn_quirk },
+};
+
+static void crb_apply_vendor_quirks(struct crb_priv *priv, struct tpm_chip *chip)
+{
+	int vendor_id = crb_vendor_id(priv);
+	unsigned int i;
+
+	if (vendor_id < 0)
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(crb_vendor_quirks); i++)
+		if (crb_vendor_quirks[i].vendor_id == vendor_id)
+			crb_vendor_quirks[i].apply(chip);
+}
+
 static int crb_acpi_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -887,6 +939,8 @@ static int crb_acpi_probe(struct platform_device *pdev)
 	chip->acpi_dev_handle = device->handle;
 	chip->flags = TPM_CHIP_FLAG_TPM2;
 
+	crb_apply_vendor_quirks(priv, chip);
+
 	rc = tpm_chip_bootstrap(chip);
 	if (rc)
 		goto out;
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/2] tpm: Add per-chip timeout for transient unavailability

In reply to Surendran Kanagaraj
Quoted 1 line · Surendran Kanagaraj

Devices that have different timeout requirements than the TPM2 spec

Can you help us understand why this device doesn't follow the spec, and why the quirk belongs in the kernel rather than being fixed in the TPM2 itself?

Is NitroTPM a virtual TPM? If so, could the virtualization software be fixed instead to follow the spec?

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz) · line 247

Quoted 6 lines
drivers/char/tpm/tpm-interface.c +2 -0
@@ -241,6 +243,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 	 */
 	memcpy(save, buf, save_size);
 
+	if (chip->busy_timeout_ms > max_delay_msec)
+		max_delay_msec = chip->busy_timeout_ms;

nit: this could use max() instead:

	max_delay_msec = max(chip->busy_timeout_ms, max_delay_msec).

Also, should there be an upper bound?

drivers/char/tpm/tpm2-cmd.c static const struct { · line 101

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +3 -1
+	if (duration < chip->busy_timeout_ms)
+		duration = chip->busy_timeout_ms;
 
-	return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
+	return msecs_to_jiffies(duration);

What about something like this instead?

return msecs_to_jiffies(max_t(unsigned long, duration,
chip->busy_timeout_ms));

Thanks,
--breno

maintainer
#8 lore raw

Re: [PATCH 2/2] tpm_crb: Raise timeouts for Amazon NitroTPM

In reply to Surendran Kanagaraj
Quoted 42 lines · Surendran Kanagaraj

TPM commands need to complete within the command duration defined in the TPM2 spec or keep answering TPM2_RC_RETRY for at most TPM2_DURATION_LONG (2s).

In rare scenarios NitroTPM can enter a mode where it either sends TPM2_RC_RETRY for more than 2s or delays command completion. When this happens during auth session start, the driver disables the chip and every subsequent request fails:

tpm tpm0: in retry loop
tpm tpm0: tpm2_load_context: failed with a TPM error 0x0922

Add support for vendor quirks to the CRB driver. The table is keyed by the vendor ID read from the interface ID register. For NitroTPM, set busy_timeout_ms to 30s, which covers the longest unavailability time along with some headroom.

Tested on an EC2 instance with NitroTPM by inducing multiple unavailability windows with no TPM errors, and in QEMU with swtpm by holding the TPM in TPM2_RC_RETRY and stalling command completion for longer than 2s with the quirk applied.

Assisted-by LLM
---
 drivers/char/tpm/tpm_crb.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/char/tpm/tpm_crb.c +1 -0
index ceb4100ba400..4107203beec9 100644
@@ -13,6 +13,7 @@
 
 #include <linux/acpi.h>
 #include <linux/highmem.h>
+#include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/rculist.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -26,6 +27,15 @@
 #define ACPI_SIG_TPM2 "TPM2"

drivers/char/tpm/tpm_crb.c line 37

Quoted 10 lines
drivers/char/tpm/tpm_crb.c +8 -0
 #define TPM_CRB_MAX_RESOURCES 3
 
+/* TPM_CRB_INTF_ID_x vendor ID field, bits 47:32 of the 64-bit register */
+#define CRB_INTF_ID_VID(intf_id)	(((intf_id) >> 32) & 0xffff)
+
+/* Amazon NitroTPM */
+#define CRB_INTF_VID_AMZN		0x0ec2
+
+/* Longest unavailability seen from NitroTPM */
+#define CRB_AMZN_BUSY_TIMEOUT_MS	30000

Use TPM_CRB prefix.

Quoted 54 lines
drivers/char/tpm/tpm_crb.c +43 -0
+
 static const guid_t crb_acpi_start_guid =
 	GUID_INIT(0x6BBF6CAB, 0x5463, 0x4714,
 		  0xB7, 0xCD, 0xF0, 0x20, 0x3C, 0x03, 0x68, 0xD4);
@@ -783,6 +793,48 @@ static int crb_map_pluton(struct device *dev, struct crb_priv *priv,
 	return 0;
 }
 
+/*
+ * Read the vendor ID from the interface ID register. Returns -ENODEV when
+ * the head registers are not mapped for this start method.
+ */
+static int crb_vendor_id(struct crb_priv *priv)
+{
+	u64 intf_id;
+
+	if (!priv->regs_h)
+		return -ENODEV;
+
+	intf_id = lo_hi_readq(&priv->regs_h->intf_id);
+
+	return CRB_INTF_ID_VID(intf_id);
+}
+
+static void crb_amzn_quirk(struct tpm_chip *chip)
+{
+	/* NitroTPM requires larger timeouts */
+	chip->busy_timeout_ms = CRB_AMZN_BUSY_TIMEOUT_MS;
+}
+
+static const struct crb_vendor_quirk {
+	u16 vendor_id;
+	void (*apply)(struct tpm_chip *chip);
+} crb_vendor_quirks[] = {
+	{ CRB_INTF_VID_AMZN, crb_amzn_quirk },
+};
+
+static void crb_apply_vendor_quirks(struct crb_priv *priv, struct tpm_chip *chip)
+{
+	int vendor_id = crb_vendor_id(priv);
+	unsigned int i;
+
+	if (vendor_id < 0)
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(crb_vendor_quirks); i++)
+		if (crb_vendor_quirks[i].vendor_id == vendor_id)
+			crb_vendor_quirks[i].apply(chip);
+}
+
 static int crb_acpi_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;

drivers/char/tpm/tpm_crb.c static int crb_acpi_probe(struct platform_device *pdev) · line 942

Quoted 4 lines
drivers/char/tpm/tpm_crb.c +1 -0
@@ -887,6 +939,8 @@ static int crb_acpi_probe(struct platform_device *pdev)
 	chip->acpi_dev_handle = device->handle;
 	chip->flags = TPM_CHIP_FLAG_TPM2;
 
+	crb_apply_vendor_quirks(priv, chip);

I.e., all this complexity equals to:

	if (crb_vendor_id(priv) == CRB_INTF_VID_AMZN)
		chip->busy_timeout_ms = CRB_AMZN_BUSY_TIMEOUT_MS;

?

Quoted 7 lines
drivers/char/tpm/tpm_crb.c +1 -0
+
 	rc = tpm_chip_bootstrap(chip);
 	if (rc)
 		goto out;
Signature · 1 line
2.47.3

Br, Jarkko

maintainer
#5 lore raw

Re: [PATCH 1/2] tpm: Add per-chip timeout for transient unavailability

In reply to Surendran Kanagaraj
Quoted 139 lines · Surendran Kanagaraj

TPM commands need to complete within the command duration defined in the TPM2 spec or to keep answering TPM2_RC_RETRY for at most TPM2_DURATION_LONG (2s).

Devices that have different timeout requirements than the TPM2 spec could exhaust the retry budget or exceed the command duration. These failures disable the device during an auth session, failing all subsequent TPM requests. Worse, when a TPM2_CC_FLUSH_CONTEXT command fails, it leaks the TPM's transient memory:

tpm tpm0: in retry loop
tpm tpm0: tpm2_load_context: failed with a TPM error 0x0922
...
tpm tpm0: A TPM error (2338) occurred flushing context

Fix this by adding chip->busy_timeout_ms to support devices that know the expected delay window. This value raises the TPM2_RC_RETRY retry budget and per-command durations to at least busy_timeout_ms. Chips that leave it at 0 keep the current timeouts. The driver sets it for NitroTPM in the following patch.

Tested with CONFIG_TCG_TPM2_HMAC=y and the following patch with the NitroTPM quirk applied, in QEMU with swtpm by stalling commands and holding the TPM in TPM2_RC_RETRY.

Assisted-by LLM
---
 drivers/char/tpm/tpm-interface.c | 11 ++++++++---
 drivers/char/tpm/tpm.h           |  2 +-
 drivers/char/tpm/tpm2-cmd.c      | 17 ++++++++++++-----
 include/linux/tpm.h              |  7 +++++++
 4 files changed, 28 insertions(+), 9 deletions(-)
drivers/char/tpm/tpm-interface.c +8 -3
index 0bab78c8767c..a423395b201a 100644
@@ -53,7 +53,7 @@ MODULE_PARM_DESC(suspend_pcr,
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 {
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
-		return tpm2_calc_ordinal_duration(ordinal);
+		return tpm2_calc_ordinal_duration(chip, ordinal);
 	else
 		return tpm1_calc_ordinal_duration(chip, ordinal);
 }
@@ -213,7 +213,8 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
  *
  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
  * the TPM and retransmits the command after a delay up to a maximum wait of
- * TPM2_DURATION_LONG.
+ * TPM2_DURATION_LONG, or chip->busy_timeout_ms when the driver declared a
+ * longer transient unavailability window.
  *
  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
  * only.
@@ -228,6 +229,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 	/* space for header and handles */
 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
 	unsigned int delay_msec = TPM2_DURATION_SHORT;
+	unsigned int max_delay_msec = TPM2_DURATION_LONG;
 	u32 rc = 0;
 	ssize_t ret;
 	const size_t save_size = min(sizeof(save), bufsiz);
@@ -241,6 +243,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 	 */
 	memcpy(save, buf, save_size);
 
+	if (chip->busy_timeout_ms > max_delay_msec)
+		max_delay_msec = chip->busy_timeout_ms;
+
 	for (;;) {
 		ret = tpm_try_transmit(chip, buf, bufsiz);
 		if (ret < 0)
@@ -255,7 +260,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
 			break;
 
-		if (delay_msec > TPM2_DURATION_LONG) {
+		if (delay_msec > max_delay_msec) {
 			if (rc == TPM2_RC_RETRY)
 				dev_err(&chip->dev, "in retry loop\n");
 			else
drivers/char/tpm/tpm.h +1 -1
index fa554c5ad80b..457eba8d03dd 100644
@@ -119,7 +119,7 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
 ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
 int tpm2_auto_startup(struct tpm_chip *chip);
 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
-unsigned long tpm2_calc_ordinal_duration(u32 ordinal);
+unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm2_probe(struct tpm_chip *chip);
 int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip);
 int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
drivers/char/tpm/tpm2-cmd.c +12 -5
index ae22295df798..dc5ed57fefe1 100644
@@ -78,20 +78,27 @@ static const struct {
 
 /**
  * tpm2_calc_ordinal_duration() - Calculate the maximum command duration
+ * @chip:	TPM chip to use.
  * @ordinal: TPM command ordinal.
  *
  * Returns the maximum amount of time the chip is expected by kernel to
- * take in jiffies.
+ * take in jiffies. The duration is never lower than chip->busy_timeout_ms.
  */
-unsigned long tpm2_calc_ordinal_duration(u32 ordinal)
+unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 {
+	unsigned long duration = TPM2_DURATION_DEFAULT;
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(tpm2_ordinal_duration_map); i++)
-		if (ordinal == tpm2_ordinal_duration_map[i].ordinal)
-			return msecs_to_jiffies(tpm2_ordinal_duration_map[i].duration);
+		if (ordinal == tpm2_ordinal_duration_map[i].ordinal) {
+			duration = tpm2_ordinal_duration_map[i].duration;
+			break;
+		}
+
+	if (duration < chip->busy_timeout_ms)
+		duration = chip->busy_timeout_ms;
 
-	return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
+	return msecs_to_jiffies(duration);
 }
 
 /**
include/linux/tpm.h +0 -0
index 0db277af45c3..7089067412d3 100644

include/linux/tpm.h struct tpm_chip { · line 148

Quoted 9 lines
include/linux/tpm.h +6 -0
@@ -140,6 +140,13 @@ struct tpm_chip {
 	unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
 	bool duration_adjusted;
 
+	/*
+	 * Longest unavailability expected from the chip in ms. Raises the
+	 * TPM2_RC_RETRY retry budget and the per-command durations to at
+	 * least this value; 0 keeps the defaults.
+	 */
+	unsigned int busy_timeout_ms;

So I think this is a wrong solution.

Instead this should be tpm_crb internal and you should simply patch chip->duration[]. See tpm_tis_core for example.

Quoted 7 lines
include/linux/tpm.h +1 -0
+
 	struct dentry *bios_dir;
 
 	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
Signature · 1 line
2.47.3

Br, Jarkko

#3 lore raw

Re: [PATCH 1/2] tpm: Add per-chip timeout for transient unavailability

In reply to Breno Leitao
Quoted 9 lines · Breno Leitao
Quoted 1 line · Surendran Kanagaraj

Devices that have different timeout requirements than the TPM2 spec

Can you help us understand why this device doesn't follow the spec, and why the quirk belongs in the kernel rather than being fixed in the TPM2 itself?

Is NitroTPM a virtual TPM? If so, could the virtualization software be fixed instead to follow the spec?

Thanks for taking time to review my patches. Yes, its a vTPM. We are also working on shortening the window on our side but its really hard to keep it under the budget for all the cases. I also came across a similar implementation in tpm_crb_ffa which retries a busy TPM based on module param busy_timeout_ms. Should we adapt this param to crb in general?

Quoted 5 lines

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz) · line 247

Quoted 2 lines
drivers/char/tpm/tpm-interface.c +2 -0
+	if (chip->busy_timeout_ms > max_delay_msec)
+		max_delay_msec = chip->busy_timeout_ms;

nit: this could use max() instead:

        max_delay_msec = max(chip->busy_timeout_ms, max_delay_msec).

Agreed, will change.

Quoted 1 line

Also, should there be an upper bound?

The value only comes from a constant in the driver So I didnt add one. I can add a upper bound.

Quoted 4 lines

What about something like this instead?

return msecs_to_jiffies(max_t(unsigned long, duration,
chip->busy_timeout_ms));

Yes that reads better.

Thanks,
Surendran

#9 lore raw

Re: [PATCH 2/2] tpm_crb: Raise timeouts for Amazon NitroTPM

In reply to Jarkko Sakkinen
Quoted 5 lines · Jarkko Sakkinen

drivers/char/tpm/tpm_crb.c line 37

Quoted 2 lines · Surendran Kanagaraj
drivers/char/tpm/tpm_crb.c +2 -0
+/* Longest unavailability seen from NitroTPM */
+#define CRB_AMZN_BUSY_TIMEOUT_MS     30000

Use TPM_CRB prefix.

Thanks for taking time to review my patches. Will update this in v2.

Quoted 8 lines

drivers/char/tpm/tpm_crb.c static int crb_acpi_probe(struct platform_device *pdev) · line 942

Quoted 1 line
drivers/char/tpm/tpm_crb.c +1 -0
+	crb_apply_vendor_quirks(priv, chip);

I.e., all this complexity equals to:

        if (crb_vendor_id(priv) == CRB_INTF_VID_AMZN)
                chip->busy_timeout_ms = CRB_AMZN_BUSY_TIMEOUT_MS;

?

Yes. I added the table to keep vendor specific quirks out of the probe and could be extended without touching the probe. tpm_tis_core does something similar with vendor_dur_overrides. what do you think? I am happy to switch to the plain check if you prefer.

Thanks,
Surendran

#6 lore raw

Re: [PATCH 1/2] tpm: Add per-chip timeout for transient unavailability

In reply to Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen

include/linux/tpm.h struct tpm_chip { · line 148

Quoted 1 line · Surendran Kanagaraj
include/linux/tpm.h +1 -0
+	unsigned int busy_timeout_ms;

So I think this is a wrong solution.

Instead this should be tpm_crb internal and you should simply patch chip->duration[]. See tpm_tis_core for example.

Thanks for the review. Agreed that this belongs in tpm_crb.

TPM2 doesn't read chip->duration[] today. Commit 207696b17f38 ("tpm: use a map for tpm2_calc_ordinal_duration()") removed the ordinal to class lookup so patching the array would mean bringing that back. Something like:

1. tpm2_get_timeouts() fills chip->duration[] with the TPM2 defaults and calls ->update_durations() the same way tpm1_get_timeouts() does. tpm_tis_update_durations() only handles 1.x, so I would make it return early on TPM2.

2. For the command durations, bring back the class lookup, so tpm2_calc_ordinal_duration() returns chip->duration[class] like tpm1_calc_ordinal_duration() does. SELF_TEST (3000 ms) and VERIFY_SIGNATURE (30000 ms) need special care since they don't fit the old classes.

3. tpm_transmit() takes the TPM2_RC_RETRY cap from chip->duration[TPM_LONG] instead of TPM2_DURATION_LONG. tpm2_get_timeouts() sets it to TPM2_DURATION_LONG, so other chips keep the same 2s.

4. tpm_crb implements ->update_durations() and raises the values to 30s for NitroTPM.

Does this match what you had in mind?

Thanks,
Surendran

maintainer
#4 lore raw

Re: [PATCH 1/2] tpm: Add per-chip timeout for transient unavailability

In reply to Surendran Kanagaraj
Quoted 11 lines · Surendran Kanagaraj
Quoted 9 lines · Breno Leitao
Quoted 1 line · Surendran Kanagaraj

Devices that have different timeout requirements than the TPM2 spec

Can you help us understand why this device doesn't follow the spec, and why the quirk belongs in the kernel rather than being fixed in the TPM2 itself?

Is NitroTPM a virtual TPM? If so, could the virtualization software be fixed instead to follow the spec?

Quoted 6 lines

Thanks for taking time to review my patches. Yes, its a vTPM. We are also working on shortening the window on our side but its really hard to keep it under the budget for all the cases. I also came across a similar implementation in tpm_crb_ffa which retries a busy TPM based on module param busy_timeout_ms. Should we adapt this param to crb in general?

Module parameter is generally a bad way to resolve anything. I don't have knowledge actually why busy_timeout_ms needs to be a module in tpm_crb_ffa and it is not possible to resolve busy_timeout_ms at run-time.

I think tpm_crb driver should simply patch chip->duration table and on TPM driver side we should start acquiring duration values by indexing that table.

E.g., consider:

	unsigned int delay_msec = TPM2_DURATION_SHORT;

We want to instead:

	unsigned int delay_msec = chip->duration[TPM_SHORT];

I.e instead of adding new fields let's take better advantage of what we have. I think this issue tells that duration table is underused and these direct assignments from constants does not scale.

Br, Jarkko

maintainer
#10 lore raw

Re: [PATCH 2/2] tpm_crb: Raise timeouts for Amazon NitroTPM

In reply to Surendran Kanagaraj
Quoted 16 lines · Surendran Kanagaraj
Quoted 5 lines · Jarkko Sakkinen

drivers/char/tpm/tpm_crb.c line 37

Quoted 2 lines · Surendran Kanagaraj
drivers/char/tpm/tpm_crb.c +2 -0
+/* Longest unavailability seen from NitroTPM */
+#define CRB_AMZN_BUSY_TIMEOUT_MS     30000

Use TPM_CRB prefix.

Thanks for taking time to review my patches. Will update this in v2.

Quoted 7 lines

drivers/char/tpm/tpm_crb.c static int crb_acpi_probe(struct platform_device *pdev) · line 942

Quoted 1 line
drivers/char/tpm/tpm_crb.c +1 -0
+	crb_apply_vendor_quirks(priv, chip);

I.e., all this complexity equals to:

        if (crb_vendor_id(priv) == CRB_INTF_VID_AMZN)
                chip->busy_timeout_ms = CRB_AMZN_BUSY_TIMEOUT_MS;
Quoted 6 lines
Quoted 1 line

?

Yes. I added the table to keep vendor specific quirks out of the probe and could be extended without touching the probe. tpm_tis_core does something similar with vendor_dur_overrides. what do you think? I am happy to switch to the plain check if you prefer.

See my suggestion for 1/2.

I.e. we need two patches:

1. One that makes all duration assignments indirect lookups to chip->duration in the main TPM driver.
2. This patch should then update that array.

Quoted 3 lines

Thanks,
Surendran

Br, Jarkko

#11 lore raw

Re: [PATCH 2/2] tpm_crb: Raise timeouts for Amazon NitroTPM

Quoted 7 lines · Jarkko Sakkinen

See my suggestion for 1/2.

I.e. we need two patches:

1. One that makes all duration assignments indirect lookups to chip->duration in the main TPM driver.
2. This patch should then update that array.

Makes sense. Thanks. I will rework it and send v2 after testing.

Thanks,
Surendran