Skip to contentSkip to conversation

tpm: fix locality and power saving handling

PATCH New Tomas Winkler 3 participants 12 messages Jan 28, 2018 – Feb 8, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tpm: fix locality and power saving handling

Devices that support locality has failed to transmit due to reserved order of locality request and cmdReady/goIdle handshake. The cmdReady/goIdle should be performed on the requested locality.

The first patch corrects the locality and power-save order, adds required polling for completion of goIdle and locality relinquish.

The second patch removes cmdRead/goIdle handshake from runtime_pm as it's rather a part of TPM2 protocol then true device power management, and on device it's no longer optional.

Tomas Winkler (2):
tpm: cmd_ready command can be issued only after granting locality
tpm: separate cmd_ready/go_idle from runtime_pm

 drivers/char/tpm/tpm-interface.c |  21 ++++--
 drivers/char/tpm/tpm_crb.c       | 157 +++++++++++++++++++++++----------------
 drivers/char/tpm/tpm_tis_core.c  |   4 +-
 include/linux/tpm.h              |   4 +-
 4 files changed, 115 insertions(+), 71 deletions(-)
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v2 1/2 tpm: cmd_ready command can be issued only after granting locality

The correct sequence is to first request locality and only after that perform cmd_ready handshake, otherwise the hardware will drop the subsequent message as from the device point of view the cmd_ready handshake wasn't performed. Symmetrically locality has to be relinquished only after going idle handshake has completed, this requires that go_idle has to poll for the completion and as well locality relinquish has to poll for completion so it is not overrriden in back to back commands flow.

The issue is only visible on devices that support multiple localities.

---
V2: poll for locality relinquish completion

 drivers/char/tpm/tpm-interface.c |  14 ++---
 drivers/char/tpm/tpm_crb.c       | 108 +++++++++++++++++++++++++++------------
 drivers/char/tpm/tpm_tis_core.c  |   4 +-
 include/linux/tpm.h              |   2 +-
 4 files changed, 87 insertions(+), 41 deletions(-)
drivers/char/tpm/tpm-interface.c +8 -6
index 76df4fbcf089..9fb3d406b078 100644
@@ -422,8 +422,6 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	if (!(flags & TPM_TRANSMIT_UNLOCKED))
 		mutex_lock(&chip->tpm_mutex);
 
-	if (chip->dev.parent)
-		pm_runtime_get_sync(chip->dev.parent);
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, true);
@@ -439,6 +437,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 		chip->locality = rc;
 	}
 
+	if (chip->dev.parent)
+		pm_runtime_get_sync(chip->dev.parent);
+
 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
 	if (rc)
 		goto out;
@@ -499,17 +500,18 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
 
 out:
+	if (chip->dev.parent)
+		pm_runtime_put_sync(chip->dev.parent);
+
 	if (need_locality && chip->ops->relinquish_locality) {
-		chip->ops->relinquish_locality(chip, chip->locality);
+		rc = chip->ops->relinquish_locality(chip, chip->locality);
 		chip->locality = -1;
 	}
+
 out_no_locality:
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
-	if (chip->dev.parent)
-		pm_runtime_put_sync(chip->dev.parent);
-
 	if (!(flags & TPM_TRANSMIT_UNLOCKED))
 		mutex_unlock(&chip->tpm_mutex);
 	return rc ? rc : len;
drivers/char/tpm/tpm_crb.c +75 -33

192 lines not on this page · raw message

drivers/char/tpm/tpm_tis_core.c +3 -1
index 183a5f54d875..a22b12adbdfd 100644
@@ -143,11 +143,13 @@ static bool check_locality(struct tpm_chip *chip, int l)
 	return false;
 }
 
-static void release_locality(struct tpm_chip *chip, int l)
+static int release_locality(struct tpm_chip *chip, int l)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 
 	tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
+
+	return 0;
 }
 
 static int request_locality(struct tpm_chip *chip, int l)
include/linux/tpm.h +1 -1
index bcdd3790e94d..06639fb6ab85 100644
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
 	void (*clk_enable)(struct tpm_chip *chip, bool value);
 };
 
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw

PATCH v2 2/2 tpm: separate cmd_ready/go_idle from runtime_pm

In reply to Tomas Winkler

We cannot use go_idle cmd_ready commands via runtime_pm handles as with the introduction of localities this is no longer an optional feature, while runtime pm can be not enabled.
Though cmd_ready/go_idle provides power saving feature, it's also part of TPM2 protocol and should be called explicitly.
This patch exposes cmd_read/go_idle via tpm class ops and removes runtime pm support as it is not used by any driver.

---
V2: resent

 drivers/char/tpm/tpm-interface.c | 15 ++++++---
 drivers/char/tpm/tpm_crb.c       | 69 +++++++++++++++++-----------------------
 include/linux/tpm.h              |  2 ++
 3 files changed, 42 insertions(+), 44 deletions(-)
drivers/char/tpm/tpm-interface.c +10 -5
index 9fb3d406b078..0c493f885a9e 100644
@@ -29,7 +29,6 @@
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
 #include <linux/freezer.h>
-#include <linux/pm_runtime.h>
 #include <linux/tpm_eventlog.h>
 
 #include "tpm.h"
@@ -437,8 +436,11 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 		chip->locality = rc;
 	}
 
-	if (chip->dev.parent)
-		pm_runtime_get_sync(chip->dev.parent);
+	if (chip->ops->cmd_ready) {
+		rc = chip->ops->cmd_ready(chip);
+		if (rc)
+			goto out;
+	}
 
 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
 	if (rc)
@@ -500,8 +502,11 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
 
 out:
-	if (chip->dev.parent)
-		pm_runtime_put_sync(chip->dev.parent);
+	if (chip->ops->go_idle) {
+		rc = chip->ops->go_idle(chip);
+		if (rc)
+			goto out;
+	}
 
 	if (need_locality && chip->ops->relinquish_locality) {
 		rc = chip->ops->relinquish_locality(chip, chip->locality);
drivers/char/tpm/tpm_crb.c +30 -39

158 lines not on this page · raw message

include/linux/tpm.h +2 -0
index 06639fb6ab85..8eb5e5ebe136 100644
@@ -43,6 +43,8 @@ struct tpm_class_ops {
 	u8 (*status) (struct tpm_chip *chip);
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
+	int (*go_idle)(struct tpm_chip *chip);
+	int (*cmd_ready)(struct tpm_chip *chip);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
 	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
 	void (*clk_enable)(struct tpm_chip *chip, bool value);
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

Re: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

In reply to Tomas Winkler

include/linux/tpm.h struct tpm_class_ops { · line 47

Quoted 9 lines · Tomas Winkler
include/linux/tpm.h +2 -1
index bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish fails?

Just returning an error code from transmit doesn't really do anything helpful from a broad subsytem perspective.

I think if a driver can fail reliquish then it needs some kind of strategy to recover.

Suggest trying the reliquish again on every next request until success, otherwise fail request locality, potentially permanently.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

RE: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

Quoted 15 lines · Tomas Winkler

include/linux/tpm.h index struct tpm_class_ops { · line 47

Quoted 10 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0

bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h

include/linux/tpm.h index renamed from include/linux/tpm.h +1 -1
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish fails?

Not much just propage the error to the caller and leave the policy decision to it.

Quoted 2 lines

Just returning an error code from transmit doesn't really do anything helpful from a broad subsytem perspective.

Yes, you are right, but I'm not sure even if the subsystem is broad enough to understand the system setup, or in another direction specific enough to behave upon hw limitations.

Quoted 3 lines

I think if a driver can fail reliquish then it needs some kind of strategy to recover.

Maybe some driver can and some not, but if it doesn't succeed it should return an error.

Quoted 3 lines

Suggest trying the reliquish again on every next request until success, otherwise fail request locality, potentially permanently.

This is something I rather prevent because it leaves the HW in kind of undefined state ( and we should probably work on that a bit more later).
As far as I've debugged the flow now, the driver just fails, and the error goes up user space caller or the internal flow is stopped.
A user can reboot the system or whatever it helps in his/her particular setup.

Make sense?

Anyhow I will dig to it more how fatal is that relinquish failure.

Thanks
Tomas

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

Quoted 5 lines · Tomas
Quoted 2 lines

I think if a driver can fail reliquish then it needs some kind of strategy to recover.

Maybe some driver can and some not, but if it doesn't succeed it should return an error.

But you can't just leave the driver in some inconsistent state..

Every time I've audited something to do with 'add error codes to destroy/free/release' I find driver design issues..

Quoted 7 lines
Quoted 2 lines

Suggest trying the reliquish again on every next request until success, otherwise fail request locality, potentially permanently.

This is something I rather prevent because it leaves the HW in kind of undefined state ( and we should probably work on that a bit more later).
As far as I've debugged the flow now, the driver just fails, and the error goes up user space caller or the internal flow is stopped.

But tranmist_command will be called again - then what does the driver do? The driver needs an answer for that..

If you don't want to retry then I'd rather see request_locality permanently fail then adding a return code to release.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

RE: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

Quoted 6 lines · Tomas
Quoted 3 lines
Quoted 3 lines

I think if a driver can fail reliquish then it needs some kind of strategy to recover.

Quoted 7 lines
Quoted 2 lines

Maybe some driver can and some not, but if it doesn't succeed it should return an error.

But you can't just leave the driver in some inconsistent state..

Every time I've audited something to do with 'add error codes to destroy/free/release' I find driver design issues..

I'm sure of it, but from this particular point the driver itself is stateless, it's just reading HW state via registers. It's not going through driver state changes.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

Suggest trying the reliquish again on every next request until success, otherwise fail request locality, potentially permanently.

This is something I rather prevent because it leaves the HW in kind of undefined state ( and we should probably work on that a bit more later). As far as I've debugged the flow now, the driver just fails, and the error goes up user space caller or the internal flow is stopped.

But tranmist_command will be called again - then what does the driver do? The driver needs an answer for that..

It will just fail again

Quoted 3 lines

If you don't want to retry then I'd rather see request_locality permanently fail then adding a return code to release.

What do you mean exactly mean by permanently fail,
My current assumption is that it will fail permanently because the HW is not responsive Or indicate error on any subsequent command, unless the hw recover somehow. Currently I'm not aware of any possibility to reset the device except rebooting the system.

Thanks
Tomas

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

In reply to Winkler, Tomas
Quoted 16 lines · Tomas
Quoted 15 lines · Tomas Winkler

include/linux/tpm.h index struct tpm_class_ops { · line 47

Quoted 10 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0

bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h

include/linux/tpm.h index renamed from include/linux/tpm.h +1 -1
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish fails?

Quoted 2 lines

Not much just propage the error to the caller and leave the policy decision to it.

Your patch set must either cover this or keep it as void.

A better idea is to print an error to klog.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

Re: [PATCH 2/2 v2] tpm: separate cmd_ready/go_idle from runtime_pm

In reply to Tomas Winkler
Quoted 9 lines · Tomas Winkler

We cannot use go_idle cmd_ready commands via runtime_pm handles as with the introduction of localities this is no longer an optional feature, while runtime pm can be not enabled.
Though cmd_ready/go_idle provides power saving feature, it's also part of TPM2 protocol and should be called explicitly.
This patch exposes cmd_read/go_idle via tpm class ops and removes runtime pm support as it is not used by any driver.

Makes sense so that only 1/2 woud need to be backported to stable (1/2 should probably have cc for stable).

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

RE: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

In reply to Jarkko Sakkinen
Quoted 17 lines · Tomas
Quoted 4 lines
Quoted 4 lines · Tomas Winkler
Quoted 11 lines
Quoted 11 lines

include/linux/tpm.h index struct tpm_class_ops { · line 47

Quoted 9 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0

bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h

include/linux/tpm.h index renamed from include/linux/tpm.h +1 -1
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish

Quoted 6 lines

fails?

Quoted 3 lines

Not much just propage the error to the caller and leave the policy decision to it.

Your patch set must either cover this or keep it as void.

How the code is covering other failures in the transmit functions, how is this one different from for example request_locality failure? Why we should not propage this error up?

Quoted 2 lines

A better idea is to print an error to klog.

We can do that in addition.

Thanks
Tomas

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

Re: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

Quoted 25 lines · Tomas
Quoted 17 lines · Tomas
Quoted 4 lines
Quoted 4 lines · Tomas Winkler
Quoted 11 lines
Quoted 11 lines

include/linux/tpm.h index struct tpm_class_ops { · line 47

Quoted 9 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0

bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h

include/linux/tpm.h index renamed from include/linux/tpm.h +1 -1
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish

Quoted 6 lines

fails?

Quoted 3 lines

Not much just propage the error to the caller and leave the policy decision to it.

Your patch set must either cover this or keep it as void.

Quoted 7 lines

How the code is covering other failures in the transmit functions, how is this one different from for example request_locality failure? Why we should not propage this error up?

Quoted 2 lines

A better idea is to print an error to klog.

We can do that in addition.

I guess you are right. This can be propagated to the user space so that it knows that there is problem. To make the root more visible the klog message would make sense.

/Jarkko
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw

RE: [PATCH 1/2 v2] tpm: cmd_ready command can be issued only after granting locality

Quoted 34 lines · Tomas
Quoted 31 lines
Quoted 18 lines · Tomas
Quoted 4 lines
Quoted 4 lines · Tomas Winkler
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0

bcdd3790e94d..06639fb6ab85 100644
+++ b/include/linux/tpm.h

include/linux/tpm.h index renamed from include/linux/tpm.h +0 -0
@@ -44,7 +44,7 @@ struct tpm_class_ops {
Quoted 8 lines

include/linux/tpm.h index struct tpm_class_ops { · line 47

Quoted 5 lines
include/linux/tpm.h index renamed from include/linux/tpm.h +1 -1
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);

This seems wrong.. What is the core code supposed to do if relinquish

Quoted 6 lines

fails?

Quoted 3 lines

Not much just propage the error to the caller and leave the policy decision to it.

Your patch set must either cover this or keep it as void.

How the code is covering other failures in the transmit functions, how is this one different from for example request_locality failure? Why we should not propage this error up?

Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

A better idea is to print an error to klog.

We can do that in addition.

I guess you are right. This can be propagated to the user space so that it knows that there is problem. To make the root more visible the klog message would make sense.

Thanks, will add an error message.
Tomas

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html