Skip to contentSkip to conversation

tpm: Cache ACPI TPM2 start method and gate tpm_crb_ffa probe

PATCH Superseded Siddharth Chintamaneni 2 participants 9 messages Nov 14

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tpm: Cache ACPI TPM2 start method and gate tpm_crb_ffa probe

This series caches the ACPI TPM2 start method in tpm_init() and uses the cached tpm2_acpi_start_method value across tpm_crb_ffa, tpm_tis, and tpm_crb.

Patch 1 caches the ACPI TPM2 start method once in tpm_init() into tpm2_acpi_start_method (defaulting to ACPI_TPM2_NOT_ALLOWED when absent or invalid).
Patch 2 checks tpm2_acpi_start_method in tpm_crb_ffa_probe() and returns -ENODEV unless ACPI advertises ACPI_TPM2_CRB_WITH_ARM_FFA, skipping FF-A probing on non-FF-A platforms while keeping modular tpm_crb/tpm_crb_ffa loading intact.
Patches 3 and 4 update tpm_tis and tpm_crb to use the cached tpm2_acpi_start_method value during probe.

Changes in v2:
- Expand into a 4-patch series caching tpm2_acpi_start_method in tpm_init() and converting tpm_crb_ffa, tpm_tis, and tpm_crb to use it (Jarkko Sakkinen).
- Move the tpm_crb_ffa start_method check from module_init into tpm_crb_ffa_probe() so tpm_crb.ko and tpm_crb_ffa.ko still load cleanly on non-FF-A systems when built as modules.

v1: https://lore.kernel.org/linux-integrity/[email protected]/

Jarkko Sakkinen (1):
tpm: Cache TPM2 start method

Siddharth Chintamaneni (3):
tpm: tpm_crb_ffa: Only probe when ACPI TPM2 uses FF-A
tpm: tpm_tis: Use cached tpm2_acpi_start_method
tpm: tpm_crb: Use cached tpm2_acpi_start_method

 drivers/char/tpm/tpm-interface.c | 21 +++++++++++++++++++++
 drivers/char/tpm/tpm.h           |  1 +
 drivers/char/tpm/tpm_crb.c       | 19 ++++++++++++-------
 drivers/char/tpm/tpm_crb_ffa.c   |  5 +++++
 drivers/char/tpm/tpm_tis.c       | 18 +++++++-----------
 5 files changed, 46 insertions(+), 18 deletions(-)

base-commit: 912c3bad2eb993eb3e83c2689cd4e17361ef087f

Signature · 1 line
2.45.4
#1 lore raw

PATCH v2 1/4 tpm: Cache TPM2 start method

From: Jarkko Sakkinen <[email protected]>

Both tpm_tis and tpm_crb retrieve the start method from the ACPI TPM2 table during device probe, and tpm_crb_ffa also needs it before chip allocation.

Cache the start method in tpm2_acpi_start_method during tpm_init() and export it for the leaf drivers, defaulting to ACPI_TPM2_NOT_ALLOWED when the table is absent or invalid.

---
 drivers/char/tpm/tpm-interface.c | 21 +++++++++++++++++++++
 drivers/char/tpm/tpm.h           |  1 +
 2 files changed, 22 insertions(+)
drivers/char/tpm/tpm-interface.c +21 -0
index e0385b16616b..7b7da79bee7c 100644
@@ -534,8 +534,17 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 }
 EXPORT_SYMBOL_GPL(tpm_get_random);
 
+#ifdef CONFIG_ACPI
+u32 tpm2_acpi_start_method = ACPI_TPM2_NOT_ALLOWED;
+EXPORT_SYMBOL_GPL(tpm2_acpi_start_method);
+#endif
+
 static int __init tpm_init(void)
 {
+#ifdef CONFIG_ACPI
+	struct acpi_table_tpm2 *tbl;
+	acpi_status status;
+#endif
 	int rc;
 
 	rc = class_register(&tpm_class);
@@ -562,6 +571,18 @@ static int __init tpm_init(void)
 		goto out_unreg_chrdev;
 	}
 
+#ifdef CONFIG_ACPI
+	if (!acpi_disabled) {
+		status = acpi_get_table(ACPI_SIG_TPM2, 1,
+					(struct acpi_table_header **)&tbl);
+		if (ACPI_SUCCESS(status)) {
+			if (tbl->header.length >= sizeof(*tbl))
+				tpm2_acpi_start_method = tbl->start_method;
+			acpi_put_table((struct acpi_table_header *)tbl);
+		}
+	}
+#endif
+
 	return 0;
 
 out_unreg_chrdev:
drivers/char/tpm/tpm.h +1 -0
index fa554c5ad80b..212ca2410c93 100644
@@ -100,6 +100,7 @@ void tpm_sysfs_add_device(struct tpm_chip *chip);
 
 
 #ifdef CONFIG_ACPI
+extern u32 tpm2_acpi_start_method;
 extern void tpm_add_ppi(struct tpm_chip *chip);
 #else
 static inline void tpm_add_ppi(struct tpm_chip *chip)
Signature · 1 line
2.45.4
maintainer
#2 lore raw

Re: [PATCH v2 1/4] tpm: Cache TPM2 start method

Quoted 42 lines · Siddharth Chintamaneni

From: Jarkko Sakkinen <[email protected]>

Both tpm_tis and tpm_crb retrieve the start method from the ACPI TPM2 table during device probe, and tpm_crb_ffa also needs it before chip allocation.

Cache the start method in tpm2_acpi_start_method during tpm_init() and export it for the leaf drivers, defaulting to ACPI_TPM2_NOT_ALLOWED when the table is absent or invalid.

---
 drivers/char/tpm/tpm-interface.c | 21 +++++++++++++++++++++
 drivers/char/tpm/tpm.h           |  1 +
 2 files changed, 22 insertions(+)
drivers/char/tpm/tpm-interface.c +9 -0
index e0385b16616b..7b7da79bee7c 100644
@@ -534,8 +534,17 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 }
 EXPORT_SYMBOL_GPL(tpm_get_random);
 
+#ifdef CONFIG_ACPI
+u32 tpm2_acpi_start_method = ACPI_TPM2_NOT_ALLOWED;
+EXPORT_SYMBOL_GPL(tpm2_acpi_start_method);
+#endif
+
 static int __init tpm_init(void)
 {
+#ifdef CONFIG_ACPI
+	struct acpi_table_tpm2 *tbl;
+	acpi_status status;
+#endif
 	int rc;
 
 	rc = class_register(&tpm_class);
@@ -562,6 +571,18 @@ static int __init tpm_init(void)
 		goto out_unreg_chrdev;

drivers/char/tpm/tpm-interface.c static int __init tpm_init(void) · line 581

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +8 -0
 	}
 
+#ifdef CONFIG_ACPI
+	if (!acpi_disabled) {
+		status = acpi_get_table(ACPI_SIG_TPM2, 1,
+					(struct acpi_table_header **)&tbl);
+		if (ACPI_SUCCESS(status)) {
+			if (tbl->header.length >= sizeof(*tbl))
+				tpm2_acpi_start_method = tbl->start_method;
+			acpi_put_table((struct acpi_table_header *)tbl);

I would add right after acpi_put_table:

	pr_info("tpm: TPM2 ACPI start method: 0%02x\n", tpm2_acpi_start_method);

It covers all three possible conditions:

1. No TPM2: nothing gets printed.
2. Has TPM2 and start method: start method gets printed.
3. Has TPM2 but no start emthod: 0x00 gets printed.

I hate fostering klog messages as we have acquired great tracing capabilities over the years but here I think having this is even "sysadmin-useful".

Br, Jarkko

maintainer
#3 lore raw

Re: [PATCH v2 1/4] tpm: Cache TPM2 start method

In reply to Siddharth Chintamaneni

drivers/char/tpm/tpm-interface.c static int __init tpm_init(void) · line 577

Quoted 2 lines · Siddharth Chintamaneni
drivers/char/tpm/tpm-interface.c +2 -0
+		status = acpi_get_table(ACPI_SIG_TPM2, 1,
+					(struct acpi_table_header **)&tbl);

Nit: no need to split into two lines and I think you can also express this in more compact way:

	status = acpi_get_table(ACPI_SIG_TPM2, 1, (typeof(*tbl) **)&tbl);

Br, Jarkko

#4 lore raw

PATCH v2 2/4 tpm: tpm_crb_ffa: Only probe when ACPI TPM2 uses FF-A

In reply to Siddharth Chintamaneni

Prior to commit 677042afb97a ("tpm: tpm_crb_ffa: revert defered_probed when tpm_crb_ffa is built-in"), built-in tpm_crb_ffa only registered tpm_crb_ffa_driver from tpm_crb_ffa_init() after crb_acpi_probe() confirmed start_method == ACPI_TPM2_CRB_WITH_ARM_FFA. After that commit, tpm_crb_ffa unconditionally registers on the FF-A bus at device_initcall time (and when loaded as a module) without checking the platform's ACPI TPM2 start method.

A platform's firmware may expose a TPM CRB over FF-A Secure Partition for UEFI boot services while advertising a non-FF-A start method to the OS in the ACPI TPM2 table. When the ACPI TPM2 table designates a non-FF-A start method, Linux has no consumer for tpm_crb_ffa and should not probe the FF-A TPM service.

On Microsoft Cobalt 200 systems, the ACPI TPM2 table does not use ACPI_TPM2_CRB_WITH_ARM_FFA, yet ffa_init() discovers the FF-A TPM partition and tpm_crb_ffa_probe() unconditionally sends CRB_FFA_GET_INTERFACE_VERSION via FFA_MSG_SEND_DIRECT_REQ2/REQ SMC. That request wedges the calling CPU in Secure World (with Non-Secure interrupts and GICv3 pseudo-NMIs masked), hanging boot with an RCU CPU stall:

  [    5.507519] ARM FF-A: Driver version 1.2
  [    5.507519] ARM FF-A: Firmware version 1.2 found
  [    5.507551] ARM FF-A: Notification setup failed -61, not enabled
  [    7.150580] Freeing initrd memory: 293640K
  [   65.515248] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
  [   65.521346] rcu:     10-...0: (3 GPs behind) idle=dd9c/1/0x4000000000000000 softirq=37/37 fqs=5262
  [   65.530031] rcu:              hardirqs   softirqs   csw/system
  [   65.535589] rcu:      number:        0          0            0
  [   65.541146] rcu:     cputime:        0          0            0   ==> 30028(ms)
  [   65.548102] rcu:     (detected by 66, t=15010 jiffies, g=-951, q=1384 ncpus=132)
  [   65.555228] Sending NMI from CPU 66 to CPUs 10:
  [   75.560686] After 10 seconds, these CPUS still haven't responded to the NMI: 10

Check tpm2_acpi_start_method in tpm_crb_ffa_probe() and return -ENODEV unless it equals ACPI_TPM2_CRB_WITH_ARM_FFA. Checking in probe() rather than module_init allows tpm_crb_ffa.ko to load as a symbol dependency of tpm_crb.ko on non-FF-A platforms when built as modules, while skipping FF-A TPM requests unless ACPI advertises the FF-A start method.

Fixes eb93f0734ef1 ("tpm_crb: ffa_tpm: Implement driver compliant to CRB over FF-A")
---
 drivers/char/tpm/tpm_crb_ffa.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/char/tpm/tpm_crb_ffa.c +5 -0
index 025c4d4b17ca..da676da6f3f0 100644
@@ -9,9 +9,11 @@
 
 #define pr_fmt(fmt) "CRB_FFA: " fmt
 
+#include <linux/acpi.h>
 #include <linux/arm_ffa.h>
 #include <linux/delay.h>
 #include <linux/moduleparam.h>
+#include "tpm.h"
 #include "tpm_crb_ffa.h"
 
 static unsigned int busy_timeout_ms = 2000;
@@ -324,6 +326,9 @@ static int tpm_crb_ffa_probe(struct ffa_device *ffa_dev)
 	struct tpm_crb_ffa *p;
 	int rc;
 
+	if (tpm2_acpi_start_method != ACPI_TPM2_CRB_WITH_ARM_FFA)
+		return -ENODEV;
+
 	/* only one instance of a TPM partition is supported */
 	if (tpm_crb_ffa && !IS_ERR_VALUE(tpm_crb_ffa))
 		return -EEXIST;
Signature · 1 line
2.45.4
#5 lore raw

PATCH v2 3/4 tpm: tpm_tis: Use cached tpm2_acpi_start_method

In reply to Siddharth Chintamaneni

Use the cached tpm2_acpi_start_method value in check_acpi_tpm2() instead of mapping and unmapping the ACPI TPM2 table again during probe.

---
 drivers/char/tpm/tpm_tis.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm_tis.c +7 -11
index 6f8344894ea5..a6fb6f889e7d 100644
@@ -138,28 +138,24 @@ MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
 static int check_acpi_tpm2(struct device *dev)
 {
 	const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
-	struct acpi_table_tpm2 *tbl;
-	acpi_status st;
-	int ret = 0;
 
 	if (!aid || aid->driver_data != DEVICE_IS_TPM2)
 		return 0;
 
-	/* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
-	 * table is mandatory
+	/*
+	 * If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
+	 * table is mandatory.
 	 */
-	st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
-	if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
+	if (tpm2_acpi_start_method == ACPI_TPM2_NOT_ALLOWED) {
 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
 		return -EINVAL;
 	}
 
 	/* The tpm2_crb driver handles this device */
-	if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
-		ret = -ENODEV;
+	if (tpm2_acpi_start_method != ACPI_TPM2_MEMORY_MAPPED)
+		return -ENODEV;
 
-	acpi_put_table((struct acpi_table_header *)tbl);
-	return ret;
+	return 0;
 }
 #else
 static int check_acpi_tpm2(struct device *dev)
Signature · 1 line
2.45.4
maintainer
#6 lore raw

Re: [PATCH v2 3/4] tpm: tpm_tis: Use cached tpm2_acpi_start_method

Quoted 22 lines · Siddharth Chintamaneni

Use the cached tpm2_acpi_start_method value in check_acpi_tpm2() instead of mapping and unmapping the ACPI TPM2 table again during probe.

---
 drivers/char/tpm/tpm_tis.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm_tis.c +0 -3
index 6f8344894ea5..a6fb6f889e7d 100644
@@ -138,28 +138,24 @@ MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
 static int check_acpi_tpm2(struct device *dev)
 
 
 
 {
 	const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
-	struct acpi_table_tpm2 *tbl;
-	acpi_status st;
-	int ret = 0;

drivers/char/tpm/tpm_tis.c MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); · line 151

Quoted 10 lines
drivers/char/tpm/tpm_tis.c +3 -2
 
 	if (!aid || aid->driver_data != DEVICE_IS_TPM2)
 		return 0;
 
-	/* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
-	 * table is mandatory
+	/*
+	 * If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
+	 * table is mandatory.
 	 */

Delete this comment entirely. It has a low value.

drivers/char/tpm/tpm_tis.c MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); · line 150

Quoted 4 lines
drivers/char/tpm/tpm_tis.c +1 -2
-	st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
-	if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
+	if (tpm2_acpi_start_method == ACPI_TPM2_NOT_ALLOWED) {
 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");

Delete dev_err()

drivers/char/tpm/tpm_tis.c MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); · line 151

Quoted 1 line
drivers/char/tpm/tpm_tis.c +0 -0
 		return -EINVAL;

-ENODEV

Quoted 11 lines
drivers/char/tpm/tpm_tis.c +3 -4
 	}
 
 	/* The tpm2_crb driver handles this device */
-	if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
-		ret = -ENODEV;
+	if (tpm2_acpi_start_method != ACPI_TPM2_MEMORY_MAPPED)
+		return -ENODEV;
 
-	acpi_put_table((struct acpi_table_header *)tbl);
-	return ret;
+	return 0;
Quoted 6 lines

}
#else
static int check_acpi_tpm2(struct device *dev)

Signature · 1 line
2.45.4

Finally after those suggestions this collapses into almost nothing i.e., please remove check_acpi_tpm2 entirely and open-code to the two call sites.

Br, Jarkko

#7 lore raw

PATCH v2 4/4 tpm: tpm_crb: Use cached tpm2_acpi_start_method

In reply to Siddharth Chintamaneni

Check the cached tpm2_acpi_start_method value at the start of crb_acpi_probe() before mapping the ACPI TPM2 table for CRB-specific parameters. This avoids mapping the table when it is absent or when the device uses ACPI_TPM2_MEMORY_MAPPED and belongs to tpm_tis.

---
 drivers/char/tpm/tpm_crb.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm_crb.c +12 -7
index 4749a9183726..c1c2b72459a2 100644
@@ -945,18 +945,23 @@ static int crb_acpi_probe(struct platform_device *pdev)
 	if (!device)
 		return -ENODEV;
 
-	status = acpi_get_table(ACPI_SIG_TPM2, 1,
-				(struct acpi_table_header **) &buf);
-	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
+	sm = tpm2_acpi_start_method;
+	if (sm == ACPI_TPM2_NOT_ALLOWED) {
 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
 		return -EINVAL;
 	}
 
 	/* Should the FIFO driver handle this? */
-	sm = buf->start_method;
-	if (sm == ACPI_TPM2_MEMORY_MAPPED) {
-		rc = -ENODEV;
-		goto out;
+	if (sm == ACPI_TPM2_MEMORY_MAPPED)
+		return -ENODEV;
+
+	status = acpi_get_table(ACPI_SIG_TPM2, 1,
+				(struct acpi_table_header **)&buf);
+	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
+		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
+		if (ACPI_SUCCESS(status))
+			acpi_put_table((struct acpi_table_header *)buf);
+		return -EINVAL;
 	}
 
 	priv = devm_kzalloc_obj(dev, struct crb_priv);
Signature · 1 line
2.45.4
maintainer
#8 lore raw

Re: [PATCH v2 4/4] tpm: tpm_crb: Use cached tpm2_acpi_start_method

Quoted 15 lines · Siddharth Chintamaneni

Check the cached tpm2_acpi_start_method value at the start of crb_acpi_probe() before mapping the ACPI TPM2 table for CRB-specific parameters. This avoids mapping the table when it is absent or when the device uses ACPI_TPM2_MEMORY_MAPPED and belongs to tpm_tis.

---
 drivers/char/tpm/tpm_crb.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm_crb.c +0 -0
index 4749a9183726..c1c2b72459a2 100644

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

Quoted 7 lines
drivers/char/tpm/tpm_crb.c +1 -3
@@ -945,18 +945,23 @@ static int crb_acpi_probe(struct platform_device *pdev)
 	if (!device)
 		return -ENODEV;
 
-	status = acpi_get_table(ACPI_SIG_TPM2, 1,
-				(struct acpi_table_header **) &buf);
-	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
+	sm = tpm2_acpi_start_method;

sm and also priv->sm should be removed

The global should be accessed across the board when querying start method. It should be the single source for it post-tpm_init.

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

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +1 -0
+	if (sm == ACPI_TPM2_NOT_ALLOWED) {
 		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");

Drop this message.

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

Quoted 1 line
drivers/char/tpm/tpm_crb.c +0 -0
 		return -EINVAL;

-ENODEV

Quoted 4 lines
drivers/char/tpm/tpm_crb.c +0 -1
 	}
 
 	/* Should the FIFO driver handle this? */
-	sm = buf->start_method;

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

Quoted 10 lines
drivers/char/tpm/tpm_crb.c +7 -3
-	if (sm == ACPI_TPM2_MEMORY_MAPPED) {
-		rc = -ENODEV;
-		goto out;
+	if (sm == ACPI_TPM2_MEMORY_MAPPED)
+		return -ENODEV;
+
+	status = acpi_get_table(ACPI_SIG_TPM2, 1,
+				(struct acpi_table_header **)&buf);
+	if (ACPI_FAILURE(status) || buf->header.length < sizeof(*buf)) {
+		dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");

Drop this message.

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

Quoted 3 lines
drivers/char/tpm/tpm_crb.c +3 -0
+		if (ACPI_SUCCESS(status))
+			acpi_put_table((struct acpi_table_header *)buf);
+		return -EINVAL;

-ENODEV

Quoted 6 lines
drivers/char/tpm/tpm_crb.c +0 -0
 	}
 
 	priv = devm_kzalloc_obj(dev, struct crb_priv);
Signature · 1 line
2.45.4

Br, Jarkko