mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: Peter Huewe <peterhuewe@gmx.de>,
	Jarkko Sakkinen <jarkko@kernel.org>,
	 Jason Gunthorpe <jgg@ziepe.ca>
Cc: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org,
	 surenkj@amazon.com, noodles@meta.com,
	 David Laight <david.laight.linux@gmail.com>,
	 Stefan Mahnke-Hartmann <stefan.mahnke-hartmann@infineon.com>,
	 Breno Leitao <leitao@debian.org>,
	kernel-team@meta.com
Subject: [PATCH v4 1/2] tpm: Disable a TPM that stops responding
Date: Fri, 20 Nov 2026 12:47:07 -0800	[thread overview]
Message-ID: <20261120-tpm_unresponsive-v4-1-b9043735ec1b@debian.org> (raw)
In-Reply-To: <20261120-tpm_unresponsive-v4-0-b9043735ec1b@debian.org>

Some TPMs stop responding at runtime, hours to months after they passed
probe. The first sign is usually "invalid TPM_STS.x 0xff", and from then
on every command fails only after the transport timeout. A TPM that is
already dead at boot fails to probe and never registers, which this
patch does not change.

The callers keep sending more commands: the hwrng thread every 10
seconds forever, and userspace through /dev/tpmrm0. Each attempt holds
tpm_mutex for the whole timeout and logs an error:

  tpm tpm0: invalid TPM_STS.x 0xff, dumping stack for forensics
  tpm tpm0: tpm_try_transmit: send(): error -62
  tpm tpm0: tpm_try_transmit: send(): error -62
  ...

The core already stops using a TPM it cannot trust: tpm_try_get_ops()
fails with -EIO once TPM_CHIP_FLAG_DISABLE is set, which
tpm2_load_null() does when it cannot restore the null key. Set the same
flag when two commands in a row time out.

The first timeout sets TPM_CHIP_FLAG_MISSED_RESPONSE, and a command that
gets an answer, whatever its TPM return code, clears it.

With interrupts, the wait helpers in tpm_tis_core, st33zp24 and
xen-tpmfront return -ETIME as soon as a signal is pending, so an -ETIME
with a signal pending is not counted. Otherwise any user could disable
the TPM by interrupting two reads of a PCR file in sysfs.

Some TPM 1.2 chips do not get ready while their self test runs, and
tpm1_do_selftest() keeps polling through those timeouts, see
commit 4643826a3da1 ("TPM: Work around buggy TPMs that block during
continue self test"). It now clears the flag after each of them.

A disabled TPM stays disabled until the driver is rebound or the machine
reboots.

Suggested-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 drivers/char/tpm/tpm-interface.c | 15 +++++++++++++++
 drivers/char/tpm/tpm1-cmd.c      |  1 +
 include/linux/tpm.h              |  1 +
 3 files changed, 17 insertions(+)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index e0385b16616b46..cb794cfc5f63e8 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -26,6 +26,7 @@
 #include <linux/spinlock.h>
 #include <linux/suspend.h>
 #include <linux/freezer.h>
+#include <linux/sched/signal.h>
 #include <linux/tpm_eventlog.h>
 
 #include "tpm.h"
@@ -268,6 +269,20 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
 		delay_msec *= 2;
 		memcpy(buf, save, save_size);
 	}
+
+	if (ret >= 0)
+		chip->flags &= ~TPM_CHIP_FLAG_MISSED_RESPONSE;
+
+	/* Some drivers also return -ETIME when a signal interrupts the wait. */
+	if (ret == -ETIME && !signal_pending(current)) {
+		if (!(chip->flags & TPM_CHIP_FLAG_MISSED_RESPONSE)) {
+			chip->flags |= TPM_CHIP_FLAG_MISSED_RESPONSE;
+		} else if (!(chip->flags & TPM_CHIP_FLAG_DISABLE)) {
+			dev_err(&chip->dev,
+				"two commands in a row timed out, disabling the TPM\n");
+			chip->flags |= TPM_CHIP_FLAG_DISABLE;
+		}
+	}
 	return ret;
 }
 
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 7a8606e8c7117b..e4e1f0799da196 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -647,6 +647,7 @@ int tpm1_do_selftest(struct tpm_chip *chip)
 		 * until the self test duration expires.
 		 */
 		if (rc == -ETIME) {
+			chip->flags &= ~TPM_CHIP_FLAG_MISSED_RESPONSE;
 			dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test");
 			tpm_msleep(delay_msec);
 			continue;
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 0db277af45c393..0f97abc82846ae 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -197,6 +197,7 @@ enum tpm_chip_flags {
 	TPM_CHIP_FLAG_HWRNG_DISABLED		= BIT(9),
 	TPM_CHIP_FLAG_DISABLE			= BIT(10),
 	TPM_CHIP_FLAG_SYNC			= BIT(11),
+	TPM_CHIP_FLAG_MISSED_RESPONSE		= BIT(12),
 };
 
 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)

-- 
2.53.1-Meta


  reply	other threads:[~2026-11-20 20:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 20:47 [PATCH v4 0/2] tpm: Stop retrying TPMs that have stopped working Breno Leitao
2026-11-20 20:47 ` Breno Leitao [this message]
2026-11-20 20:47 ` [PATCH v4 2/2] tpm: Disable TPM on TPM_RC_INITIALIZE Breno Leitao
2026-11-20 21:29 ` [PATCH v4 0/2] tpm: Stop retrying TPMs that have stopped working Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-tpm_unresponsive-v4-1-b9043735ec1b@debian.org \
    --to=leitao@debian.org \
    --cc=david.laight.linux@gmail.com \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=kernel-team@meta.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=noodles@meta.com \
    --cc=peterhuewe@gmx.de \
    --cc=stefan.mahnke-hartmann@infineon.com \
    --cc=surenkj@amazon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®