Skip to contentSkip to conversation

keys/trusted_keys: reuse TPM version in option handling

PATCH Superseded Srish Srinivasan 4 participants 6 messages Oct 2 – Oct 5

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 keys/trusted_keys: reuse TPM version in option handling

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

---
Changelog:

v2:
- Use bool to indicate whether the chip is TPM 2.0 in the option helpers

 security/keys/trusted-keys/trusted_tpm1.c | 35 +++++++++--------------
 1 file changed, 13 insertions(+), 22 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +13 -22
index 3ec078ca3f97..6dbce058368f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
 /* can have zero or more token= options */
 static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, bool is_tpm2)
 {
 	struct trusted_key_tpm *private = opt->private;
 	substring_t args[MAX_OPT_ARGS];
@@ -712,13 +712,8 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	unsigned long token_mask = 0;
 	unsigned int digest_len;
 	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
-	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
+	private->hash = is_tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 
 	if (!c)
 		return 0;
@@ -773,7 +768,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 				break;
 			}
 
-			if (tpm2 &&
+			if (is_tpm2 &&
 			    private->blobauth_len <=
 			    sizeof(private->blobauth)) {
 				memcpy(private->blobauth, args[0].from,
@@ -808,14 +803,15 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 			}
 			if (i == HASH_ALGO__LAST)
 				return -EINVAL;
-			if  (!tpm2 && i != HASH_ALGO_SHA1) {
+			if (!is_tpm2 && i != HASH_ALGO_SHA1) {
 				pr_info("TPM 1.x only supports SHA-1.\n");
 				return -EINVAL;
 			}
 			break;
 		case Opt_policydigest:
 			digest_len = hash_digest_size[private->hash];
-			if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
+			if (!is_tpm2 ||
+			    strlen(args[0].from) != (2 * digest_len))
 				return -EINVAL;
 			res = hex2bin(private->policydigest, args[0].from,
 				      digest_len);
@@ -824,7 +820,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 			private->policydigest_len = digest_len;
 			break;
 		case Opt_policyhandle:
-			if (!tpm2)
+			if (!is_tpm2)
 				return -EINVAL;
 			res = kstrtoul(args[0].from, 16, &handle);
 			if (res < 0)
@@ -838,15 +834,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	return 0;
 }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(bool is_tpm2)
 {
 	struct trusted_key_tpm *private;
 	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
 	options = kzalloc_obj(*options);
 	if (options) {
@@ -858,7 +849,7 @@ static struct trusted_key_options *trusted_options_alloc(void)
 			kfree_sensitive(options);
 			options = NULL;
 		} else {
-			if (!tpm2)
+			if (!is_tpm2)
 				private->keyhandle = SRKHANDLE;
 			options->private = private;
 		}
@@ -877,13 +868,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
@@ -922,12 +913,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
Signature · 1 line
2.53.0
#1 lore raw
Quoted 3 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

So this is a bit nitpicking perhaps but a patch that would just clean the code up a bit would not be be worth of applying. Almost anything that "does nothing" goes to that bin.

What I'm saying is that the conclusion in the last sentence goes to wrong direction but luckily the change has also feasible effects that we care about.

By caching tpm_is_tpm2() there are less fallible sites in the key creation process, and reduced number of roundtrips with the TPM chip.

Or from blackbox perspective it reduces I/O traffic between kernel and the hardware platform.

Quoted 136 lines

---
Changelog:

v2:
- Use bool to indicate whether the chip is TPM 2.0 in the option helpers

 security/keys/trusted-keys/trusted_tpm1.c | 35 +++++++++--------------
 1 file changed, 13 insertions(+), 22 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +13 -22
index 3ec078ca3f97..6dbce058368f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
 /* can have zero or more token= options */
 static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, bool is_tpm2)
 {
 	struct trusted_key_tpm *private = opt->private;
 	substring_t args[MAX_OPT_ARGS];
@@ -712,13 +712,8 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	unsigned long token_mask = 0;
 	unsigned int digest_len;
 	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
-	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
+	private->hash = is_tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 
 	if (!c)
 		return 0;
@@ -773,7 +768,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 				break;
 			}
 
-			if (tpm2 &&
+			if (is_tpm2 &&
 			    private->blobauth_len <=
 			    sizeof(private->blobauth)) {
 				memcpy(private->blobauth, args[0].from,
@@ -808,14 +803,15 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 			}
 			if (i == HASH_ALGO__LAST)
 				return -EINVAL;
-			if  (!tpm2 && i != HASH_ALGO_SHA1) {
+			if (!is_tpm2 && i != HASH_ALGO_SHA1) {
 				pr_info("TPM 1.x only supports SHA-1.\n");
 				return -EINVAL;
 			}
 			break;
 		case Opt_policydigest:
 			digest_len = hash_digest_size[private->hash];
-			if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
+			if (!is_tpm2 ||
+			    strlen(args[0].from) != (2 * digest_len))
 				return -EINVAL;
 			res = hex2bin(private->policydigest, args[0].from,
 				      digest_len);
@@ -824,7 +820,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 			private->policydigest_len = digest_len;
 			break;
 		case Opt_policyhandle:
-			if (!tpm2)
+			if (!is_tpm2)
 				return -EINVAL;
 			res = kstrtoul(args[0].from, 16, &handle);
 			if (res < 0)
@@ -838,15 +834,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	return 0;
 }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(bool is_tpm2)
 {
 	struct trusted_key_tpm *private;
 	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
 	options = kzalloc_obj(*options);
 	if (options) {
@@ -858,7 +849,7 @@ static struct trusted_key_options *trusted_options_alloc(void)
 			kfree_sensitive(options);
 			options = NULL;
 		} else {
-			if (!tpm2)
+			if (!is_tpm2)
 				private->keyhandle = SRKHANDLE;
 			options->private = private;
 		}
@@ -877,13 +868,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
@@ -922,12 +913,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
Signature · 1 line
2.53.0

Br, Jarkko

#2 lore raw

Hi Jarkko,

Quoted 12 lines · Jarkko Sakkinen
Quoted 3 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

So this is a bit nitpicking perhaps but a patch that would just clean the code up a bit would not be be worth of applying. Almost anything that "does nothing" goes to that bin.

What I'm saying is that the conclusion in the last sentence goes to wrong direction but luckily the change has also feasible effects that we care about.

Quoted 5 lines

By caching tpm_is_tpm2() there are less fallible sites in the key creation process, and reduced number of roundtrips with the TPM chip.

Or from blackbox perspective it reduces I/O traffic between kernel and the hardware platform.

Yes, that makes sense. Thank you for pointing this out.

I will revise the commit message to highlight the reduced failure surface and I/O between the kernel and the TPM, and send v3.

Quoted 128 lines
Quoted 128 lines

---
Changelog:

v2:
- Use bool to indicate whether the chip is TPM 2.0 in the option helpers

  security/keys/trusted-keys/trusted_tpm1.c | 35 +++++++++--------------
  1 file changed, 13 insertions(+), 22 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +12 -21
index 3ec078ca3f97..6dbce058368f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
  /* can have zero or more token= options */
  static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, bool is_tpm2)
  {
  	struct trusted_key_tpm *private = opt->private;
  	substring_t args[MAX_OPT_ARGS];
@@ -712,13 +712,8 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	unsigned long token_mask = 0;
  	unsigned int digest_len;
  	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
-	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
+	private->hash = is_tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 
  	if (!c)
  		return 0;
@@ -773,7 +768,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  				break;
  			}
 
-			if (tpm2 &&
+			if (is_tpm2 &&
  			    private->blobauth_len <=
  			    sizeof(private->blobauth)) {
  				memcpy(private->blobauth, args[0].from,
@@ -808,14 +803,15 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  			}
  			if (i == HASH_ALGO__LAST)
  				return -EINVAL;
-			if  (!tpm2 && i != HASH_ALGO_SHA1) {
+			if (!is_tpm2 && i != HASH_ALGO_SHA1) {
  				pr_info("TPM 1.x only supports SHA-1.\n");
  				return -EINVAL;
  			}
  			break;
  		case Opt_policydigest:
  			digest_len = hash_digest_size[private->hash];
-			if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
+			if (!is_tpm2 ||
+			    strlen(args[0].from) != (2 * digest_len))
  				return -EINVAL;
  			res = hex2bin(private->policydigest, args[0].from,
  				      digest_len);
@@ -824,7 +820,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  			private->policydigest_len = digest_len;
  			break;
  		case Opt_policyhandle:
-			if (!tpm2)
+			if (!is_tpm2)
  				return -EINVAL;
  			res = kstrtoul(args[0].from, 16, &handle);
  			if (res < 0)
@@ -838,15 +834,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	return 0;
  }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(bool is_tpm2)
  {
  	struct trusted_key_tpm *private;
  	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
  	options = kzalloc_obj(*options);
  	if (options) {
@@ -858,7 +849,7 @@ static struct trusted_key_options *trusted_options_alloc(void)
  			kfree_sensitive(options);
  			options = NULL;
  		} else {
-			if (!tpm2)
+			if (!is_tpm2)
  				private->keyhandle = SRKHANDLE;
  			options->private = private;
  		}
@@ -877,13 +868,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
 
  	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
@@ -922,12 +913,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
  	private = options->private;
 
Quoted 9 lines
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm1.c +1 -1
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
Signature · 1 line
2.53.0

Br, Jarkko

Thanks,
Srish

maintainer
#3 lore raw
Quoted 22 lines · Srish Srinivasan

Hi Jarkko,

Quoted 12 lines · Jarkko Sakkinen
Quoted 3 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

So this is a bit nitpicking perhaps but a patch that would just clean the code up a bit would not be be worth of applying. Almost anything that "does nothing" goes to that bin.

What I'm saying is that the conclusion in the last sentence goes to wrong direction but luckily the change has also feasible effects that we care about.

Quoted 5 lines

By caching tpm_is_tpm2() there are less fallible sites in the key creation process, and reduced number of roundtrips with the TPM chip.

Or from blackbox perspective it reduces I/O traffic between kernel and the hardware platform.

Quoted 5 lines

Yes, that makes sense. Thank you for pointing this out.

I will revise the commit message to highlight the reduced failure surface and I/O between the kernel and the TPM, and send v3.

And I've seen chips where every round trip is a roll of the dice as to whether the chip will become useless till next reboot, so that is a bigger win than I was first thinking.

Quoted 128 lines
Quoted 117 lines
Quoted 117 lines

---
Changelog:

v2:
- Use bool to indicate whether the chip is TPM 2.0 in the option helpers

  security/keys/trusted-keys/trusted_tpm1.c | 35 +++++++++--------------
  1 file changed, 13 insertions(+), 22 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +12 -22
index 3ec078ca3f97..6dbce058368f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
  /* can have zero or more token= options */
  static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, bool is_tpm2)
  {
  	struct trusted_key_tpm *private = opt->private;
  	substring_t args[MAX_OPT_ARGS];
@@ -712,13 +712,8 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	unsigned long token_mask = 0;
  	unsigned int digest_len;
  	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
-	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
+	private->hash = is_tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
  	if (!c)
  		return 0;
@@ -773,7 +768,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  				break;
  			}
-			if (tpm2 &&
+			if (is_tpm2 &&
  			    private->blobauth_len <=
  			    sizeof(private->blobauth)) {
  				memcpy(private->blobauth, args[0].from,
@@ -808,14 +803,15 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  			}
  			if (i == HASH_ALGO__LAST)
  				return -EINVAL;
-			if  (!tpm2 && i != HASH_ALGO_SHA1) {
+			if (!is_tpm2 && i != HASH_ALGO_SHA1) {
  				pr_info("TPM 1.x only supports SHA-1.\n");
  				return -EINVAL;
  			}
  			break;
  		case Opt_policydigest:
  			digest_len = hash_digest_size[private->hash];
-			if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
+			if (!is_tpm2 ||
+			    strlen(args[0].from) != (2 * digest_len))
  				return -EINVAL;
  			res = hex2bin(private->policydigest, args[0].from,
  				      digest_len);
@@ -824,7 +820,7 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  			private->policydigest_len = digest_len;
  			break;
  		case Opt_policyhandle:
-			if (!tpm2)
+			if (!is_tpm2)
  				return -EINVAL;
  			res = kstrtoul(args[0].from, 16, &handle);
  			if (res < 0)
@@ -838,15 +834,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	return 0;
  }
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(bool is_tpm2)
  {
  	struct trusted_key_tpm *private;
  	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
  	options = kzalloc_obj(*options);
  	if (options) {
@@ -858,7 +849,7 @@ static struct trusted_key_options *trusted_options_alloc(void)
  			kfree_sensitive(options);
  			options = NULL;
  		} else {
-			if (!tpm2)
+			if (!is_tpm2)
  				private->keyhandle = SRKHANDLE;
  			options->private = private;
  		}
@@ -877,13 +868,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
  	private = options->private;
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
@@ -922,12 +913,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
  	private = options->private;
-	ret = getoptions(datablob, p, options);
Quoted 8 lines
Quoted 7 lines
security/keys/trusted-keys/trusted_tpm1.c +1 -0
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
Signature · 1 line
2.53.0

Br, Jarkko

Thanks,
Srish

maintainer
#4 lore raw
Quoted 25 lines · Serge E. Hallyn
Quoted 24 lines · Srish Srinivasan

Hi Jarkko,

Quoted 12 lines · Jarkko Sakkinen
Quoted 3 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

So this is a bit nitpicking perhaps but a patch that would just clean the code up a bit would not be be worth of applying. Almost anything that "does nothing" goes to that bin.

What I'm saying is that the conclusion in the last sentence goes to wrong direction but luckily the change has also feasible effects that we care about.

Quoted 5 lines

By caching tpm_is_tpm2() there are less fallible sites in the key creation process, and reduced number of roundtrips with the TPM chip.

Or from blackbox perspective it reduces I/O traffic between kernel and the hardware platform.

Yes, that makes sense. Thank you for pointing this out.

Quoted 7 lines
Quoted 3 lines

I will revise the commit message to highlight the reduced failure surface and I/O between the kernel and the TPM, and send v3.

And I've seen chips where every round trip is a roll of the dice as to whether the chip will become useless till next reboot, so that is a bigger win than I was first thinking.

This is true and happens quite often. And in CPU companies in fact all the time with early FPGA implementations and very early ASIC based software development platforms.

We also have opt-in bus encryption feature where extra roundtrips have a horrible time cost.

Roundtrip removals are high value patches for both R&D and production, and they are always most welcome.

Br, Jarkko

maintainer
#5 lore raw
Quoted 38 lines · Jarkko Sakkinen
Quoted 25 lines · Serge E. Hallyn
Quoted 24 lines · Srish Srinivasan

Hi Jarkko,

Quoted 12 lines · Jarkko Sakkinen
Quoted 3 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

So this is a bit nitpicking perhaps but a patch that would just clean the code up a bit would not be be worth of applying. Almost anything that "does nothing" goes to that bin.

What I'm saying is that the conclusion in the last sentence goes to wrong direction but luckily the change has also feasible effects that we care about.

Quoted 5 lines

By caching tpm_is_tpm2() there are less fallible sites in the key creation process, and reduced number of roundtrips with the TPM chip.

Or from blackbox perspective it reduces I/O traffic between kernel and the hardware platform.

Yes, that makes sense. Thank you for pointing this out.

Quoted 7 lines
Quoted 3 lines

I will revise the commit message to highlight the reduced failure surface and I/O between the kernel and the TPM, and send v3.

And I've seen chips where every round trip is a roll of the dice as to whether the chip will become useless till next reboot, so that is a bigger win than I was first thinking.

This is true and happens quite often. And in CPU companies in fact all the time with early FPGA implementations and very early ASIC based software development platforms.

Quoted 5 lines

We also have opt-in bus encryption feature where extra roundtrips have a horrible time cost.

Roundtrip removals are high value patches for both R&D and production, and they are always most welcome.

Serge, your comment reminded me of hwrng. In my opinion TPM driver should limit itself randomness supply because it could have positive effect on some issues that people are having [1]. hwrng can never properly control TPM as it is behind abstraction.

I think there should be a tpm_chip associated pool of randomness. This pool is filled with some timer triggering with a constant frequency. On each iteration, it is filled with best effort i.e. with a single TPM2_GetRandom invocation.

Then hwrng_fill is also filled with best effort from the pool without any TPM interaction.

This would be an architecture that would be vastly easier to deduce than the one which we have now, which is more unstable, which is bad considering e.g., latency peaks and whatnot.

This basic idea can be improved with a low watermark: when going below it hwrng_fillfn would start the timer. I don't think high watermark is needed as pool can be always filled just up to its size.

I had this idea already 3-4 years ago but had forgotten about it.

[1] https://bugzilla.kernel.org/show_bug.cgi?id=217890

Br, Jarkkko