Skip to contentSkip to conversation

tpm: cmd_ready command can be issued only after granting locality

PATCH Superseded Tomas Winkler 3 participants 12 messages Jan 23, 2018 – Feb 1, 2018

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 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.

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

---
 drivers/char/tpm/tpm-interface.c | 12 ++---
 drivers/char/tpm/tpm_crb.c       | 95 ++++++++++++++++++++++++++--------------
 2 files changed, 70 insertions(+), 37 deletions(-)
drivers/char/tpm/tpm-interface.c +7 -5
index 76df4fbcf089..197fda39aab5 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);
 		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 +63 -32

182 lines not on this page · raw message

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 2/3 tpm: separate cmd_ready/go_idle from runtime_pm

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.

---
 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 197fda39aab5..e4d0291e22f6 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) {
 		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 bcdd3790e94d..5c9c5a106350 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);
 	void (*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
#5 lore raw

PATCH 3/3 tpm: add longer timeouts for creation commands.

In reply to Tomas Winkler

TPM2_CC_Create(0x153) and TPM2_CC_CreatePrimary (0x131) involve generation of crypto keys which can be a computationally intensive task. The timeout is set to 3min.

---
 drivers/char/tpm/tpm-interface.c |  4 ++++
 drivers/char/tpm/tpm.h           | 27 ++++++++++++++++-----------
 drivers/char/tpm/tpm2-cmd.c      |  8 +++++---
 3 files changed, 25 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-interface.c +4 -0
index e4d0291e22f6..4c6334f0f9ad 100644
@@ -673,6 +673,10 @@ int tpm_get_timeouts(struct tpm_chip *chip)
 		    msecs_to_jiffies(TPM2_DURATION_MEDIUM);
 		chip->duration[TPM_LONG] =
 		    msecs_to_jiffies(TPM2_DURATION_LONG);
+		chip->duration[TPM_LONG_LONG] =
+		    msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
+		chip->duration[TPM_UNDEFINED] =
+		    msecs_to_jiffies(TPM2_DURATION_DEFAULT);
 
 		chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
 		return 0;
drivers/char/tpm/tpm.h +16 -11
index f895fba4e20d..192ba68b39c2 100644
@@ -67,7 +67,9 @@ enum tpm_duration {
 	TPM_SHORT = 0,
 	TPM_MEDIUM = 1,
 	TPM_LONG = 2,
-	TPM_UNDEFINED,
+	TPM_LONG_LONG = 3,
+	TPM_UNDEFINED = 4,
+	TPM_DURATION_MAX,
 };
 
 #define TPM_WARN_RETRY          0x800
@@ -79,15 +81,17 @@ enum tpm_duration {
 #define TPM_HEADER_SIZE		10
 
 enum tpm2_const {
-	TPM2_PLATFORM_PCR	= 24,
-	TPM2_PCR_SELECT_MIN	= ((TPM2_PLATFORM_PCR + 7) / 8),
-	TPM2_TIMEOUT_A		= 750,
-	TPM2_TIMEOUT_B		= 2000,
-	TPM2_TIMEOUT_C		= 200,
-	TPM2_TIMEOUT_D		= 30,
-	TPM2_DURATION_SHORT	= 20,
-	TPM2_DURATION_MEDIUM	= 750,
-	TPM2_DURATION_LONG	= 2000,
+	TPM2_PLATFORM_PCR       =     24,
+	TPM2_PCR_SELECT_MIN     = ((TPM2_PLATFORM_PCR + 7) / 8),
+	TPM2_TIMEOUT_A          =    750,
+	TPM2_TIMEOUT_B          =   2000,
+	TPM2_TIMEOUT_C          =    200,
+	TPM2_TIMEOUT_D          =     30,
+	TPM2_DURATION_SHORT     =     20,
+	TPM2_DURATION_MEDIUM    =    750,
+	TPM2_DURATION_LONG      =   2000,
+	TPM2_DURATION_LONG_LONG = 300000,
+	TPM2_DURATION_DEFAULT   = 120000,
 };
 
 enum tpm2_structures {
@@ -123,6 +127,7 @@ enum tpm2_algorithms {
 
 enum tpm2_command_codes {
 	TPM2_CC_FIRST		= 0x011F,
+	TPM2_CC_CREATE_PRIMARY  = 0x0131,
 	TPM2_CC_SELF_TEST	= 0x0143,
 	TPM2_CC_STARTUP		= 0x0144,
 	TPM2_CC_SHUTDOWN	= 0x0145,
@@ -227,7 +232,7 @@ struct tpm_chip {
 	unsigned long timeout_c; /* jiffies */
 	unsigned long timeout_d; /* jiffies */
 	bool timeout_adjusted;
-	unsigned long duration[3]; /* jiffies */
+	unsigned long duration[TPM_DURATION_MAX]; /* jiffies */
 	bool duration_adjusted;
 
 	struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
drivers/char/tpm/tpm2-cmd.c +5 -3
index c17e75348a99..aaa17e982b37 100644
@@ -90,6 +90,8 @@ static struct tpm2_hash tpm2_hash_map[] = {
  * of time the chip could take to return the result. The values
  * of the SHORT, MEDIUM, and LONG durations are taken from the
  * PC Client Profile (PTP) specification.
+ * LONG_LONG is for commands that generates keys which empirically
+ * takes longer time on some systems.
  */
 static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
 	TPM_UNDEFINED,		/* 11F */
@@ -110,7 +112,7 @@ static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
 	TPM_UNDEFINED,		/* 12e */
 	TPM_UNDEFINED,		/* 12f */
 	TPM_UNDEFINED,		/* 130 */
-	TPM_UNDEFINED,		/* 131 */
+	TPM_LONG_LONG,		/* 131 */
 	TPM_UNDEFINED,		/* 132 */
 	TPM_UNDEFINED,		/* 133 */
 	TPM_UNDEFINED,		/* 134 */
@@ -144,7 +146,7 @@ static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
 	TPM_UNDEFINED,		/* 150 */
 	TPM_UNDEFINED,		/* 151 */
 	TPM_UNDEFINED,		/* 152 */
-	TPM_UNDEFINED,		/* 153 */
+	TPM_LONG_LONG,		/* 153 */
 	TPM_UNDEFINED,		/* 154 */
 	TPM_UNDEFINED,		/* 155 */
 	TPM_UNDEFINED,		/* 156 */
@@ -817,7 +819,7 @@ unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
 		duration = chip->duration[index];
 
 	if (duration <= 0)
-		duration = 2 * 60 * HZ;
+		duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
 
 	return duration;
 }
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@ http://vger.kernel.org/majordomo-info.html
#8 lore raw
In reply to Tomas Winkler
Quoted 10 lines · Tomas Winkler

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.

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

Thank you!

Quoted 65 lines
---
 drivers/char/tpm/tpm-interface.c | 12 ++---
 drivers/char/tpm/tpm_crb.c       | 95 ++++++++++++++++++++++++++--------------
 2 files changed, 70 insertions(+), 37 deletions(-)
drivers/char/tpm/tpm-interface.c +7 -5
index 76df4fbcf089..197fda39aab5 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);
 		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 +7 -0
index 7b3c2a8aa9de..d174919c446d 100644
@@ -112,6 +112,25 @@ struct tpm2_crb_smc {
 	u32 smc_func_id;
 };
 
+static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
+				unsigned long timeout)
+{
+	ktime_t start;
+	ktime_t stop;
+
+	start = ktime_get();

drivers/char/tpm/tpm_crb.c struct tpm2_crb_smc { · line 131

Quoted 10 lines
drivers/char/tpm/tpm_crb.c +10 -0
+	stop = ktime_add(start, ms_to_ktime(timeout));
+
+	do {
+		if ((ioread32(reg) & mask) == value)
+			return true;
+
+		usleep_range(50, 100);
+	} while (ktime_before(ktime_get(), stop));
+
+	return ((ioread32(reg) & mask) == value);

Not sure about the change of return statement. Is this worth of doing?

Quoted 153 lines
drivers/char/tpm/tpm_crb.c +40 -29
+}
+
 /**
  * crb_go_idle - request tpm crb device to go the idle state
  *
@@ -128,7 +147,7 @@ struct tpm2_crb_smc {
  *
  * Return: 0 always
  */
-static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
+static int crb_go_idle(struct device *dev, struct crb_priv *priv)
 {
 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
@@ -136,30 +155,17 @@ static int __maybe_unused crb_go_idle(struct device *dev, struct crb_priv *priv)
 		return 0;
 
 	iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
-	/* we don't really care when this settles */
 
+	if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
+				 CRB_CTRL_REQ_GO_IDLE/* mask */,
+				 0, /* value */
+				 TPM2_TIMEOUT_C)) {
+		dev_warn(dev, "goIdle timed out\n");
+		return -ETIME;
+	}
 	return 0;
 }
 
-static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
-				unsigned long timeout)
-{
-	ktime_t start;
-	ktime_t stop;
-
-	start = ktime_get();
-	stop = ktime_add(start, ms_to_ktime(timeout));
-
-	do {
-		if ((ioread32(reg) & mask) == value)
-			return true;
-
-		usleep_range(50, 100);
-	} while (ktime_before(ktime_get(), stop));
-
-	return false;
-}
-
 /**
  * crb_cmd_ready - request tpm crb device to enter ready state
  *
@@ -175,8 +181,7 @@ static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
  *
  * Return: 0 on success -ETIME on timeout;
  */
-static int __maybe_unused crb_cmd_ready(struct device *dev,
-					struct crb_priv *priv)
+static int crb_cmd_ready(struct device *dev, struct crb_priv *priv)
 {
 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
 	    (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
@@ -195,11 +200,11 @@ static int __maybe_unused crb_cmd_ready(struct device *dev,
 	return 0;
 }
 
-static int crb_request_locality(struct tpm_chip *chip, int loc)
+static int __crb_request_locality(struct device *dev,
+				  struct crb_priv *priv, int loc)
 {
-	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
 	u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
-		CRB_LOC_STATE_TPM_REG_VALID_STS;
+		    CRB_LOC_STATE_TPM_REG_VALID_STS;
 
 	if (!priv->regs_h)
 		return 0;
@@ -207,23 +212,36 @@ static int crb_request_locality(struct tpm_chip *chip, int loc)
 	iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h->loc_ctrl);
 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
 				 TPM2_TIMEOUT_C)) {
-		dev_warn(&chip->dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
+		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed out\n");
 		return -ETIME;
 	}
 
 	return 0;
 }
 
-static void crb_relinquish_locality(struct tpm_chip *chip, int loc)
+static int crb_request_locality(struct tpm_chip *chip, int loc)
 {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
 
+	return __crb_request_locality(&chip->dev, priv, loc);
+}
+
+static void __crb_relinquish_locality(struct device *dev,
+				      struct crb_priv *priv, int loc)
+{
 	if (!priv->regs_h)
 		return;
 
 	iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);
 }
 
+static void crb_relinquish_locality(struct tpm_chip *chip, int loc)
+{
+	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
+
+	__crb_relinquish_locality(&chip->dev, priv, loc);
+}
+
 static u8 crb_status(struct tpm_chip *chip)
 {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
@@ -475,6 +493,10 @@ static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
 			dev_warn(dev, FW_BUG "Bad ACPI memory layout");
 	}
 
+	ret = __crb_request_locality(dev, priv, 0);
+	if (ret)
+		return ret;
+
 	priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
 				   sizeof(struct crb_regs_tail));
 	if (IS_ERR(priv->regs_t))
@@ -531,6 +553,8 @@ static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
 
 	crb_go_idle(dev, priv);
 
+	__crb_relinquish_locality(dev, priv, 0);
+
 	return ret;
 }
 
@@ -588,10 +612,14 @@ static int crb_acpi_add(struct acpi_device *device)
 	chip->acpi_dev_handle = device->handle;
 	chip->flags = TPM_CHIP_FLAG_TPM2;
 
-	rc  = crb_cmd_ready(dev, priv);
+	rc = __crb_request_locality(dev, priv, 0);
 	if (rc)
 		return rc;
 
+	rc  = crb_cmd_ready(dev, priv);
+	if (rc)
+		goto out;
+
 	pm_runtime_get_noresume(dev);
 	pm_runtime_set_active(dev);
 	pm_runtime_enable(dev);

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device *device) · line 635

Quoted 9 lines
drivers/char/tpm/tpm_crb.c +2 -2
@@ -601,12 +629,15 @@ static int crb_acpi_add(struct acpi_device *device)
 		crb_go_idle(dev, priv);
 		pm_runtime_put_noidle(dev);
 		pm_runtime_disable(dev);
-		return rc;
+		goto out;
 	}
 
-	pm_runtime_put(dev);
+	pm_runtime_put_sync(dev);

Change to put_sync is needed so that the idle handshake gets completed?

Quoted 12 lines
drivers/char/tpm/tpm_crb.c +4 -1
-	return 0;
+out:
+	__crb_relinquish_locality(dev, priv, 0);
+
+	return rc;
 }
 
 static int crb_acpi_remove(struct acpi_device *device)
Signature · 1 line
2.14.3

/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

#2 lore raw

Re: [PATCH 2/3] 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.

Thank you.

LGTM

Jason, what do you think?

/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

#6 lore raw

Re: [PATCH 3/3] tpm: add longer timeouts for creation commands.

In reply to Tomas Winkler
Quoted 7 lines · Tomas Winkler

TPM2_CC_Create(0x153) and TPM2_CC_CreatePrimary (0x131) involve generation of crypto keys which can be a computationally intensive task. The timeout is set to 3min.

From where are these numbers are derived from?

Can you send 1/3 and 2/3 as a separate patch set with a cover letter (with explanation) and this as a separate patch? This is unrelated to first two changes albeit might 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

#3 lore raw

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

In reply to Jarkko Sakkinen
Quoted 9 lines · Jarkko Sakkinen
Quoted 8 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.

Quoted 7 lines
Quoted 1 line

Thank you.

LGTM

Jason, what do you think?

The PM stuff has been the source of confusion for a while, seems reasonable to get rid of it.

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

#9 lore raw
In reply to Jarkko Sakkinen
Quoted 88 lines
Quoted 11 lines

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.

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

Thank you!

Quoted 3 lines
---
 drivers/char/tpm/tpm-interface.c | 12 ++---
 drivers/char/tpm/tpm_crb.c       | 95 ++++++++++++++++++++++++++---------

-----

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, struct

Quoted 8 lines
 2 files changed, 70 insertions(+), 37 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -0
index 76df4fbcf089..197fda39aab5 100644
@@ -422,8 +422,6 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct

tpm_space *space,

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip, struct

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -2
 	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,

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit(struct tpm_chip *chip,

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +3 -0
 		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,

Quoted 40 lines
drivers/char/tpm/tpm-interface.c +4 -3
 	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);
 		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 +11 -0
index 7b3c2a8aa9de..d174919c446d 100644
@@ -112,6 +112,25 @@ struct tpm2_crb_smc {
 	u32 smc_func_id;
 };
 
+static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
+				unsigned long timeout)
+{
+	ktime_t start;
+	ktime_t stop;
+
+	start = ktime_get();
+	stop = ktime_add(start, ms_to_ktime(timeout));
+
+	do {
+		if ((ioread32(reg) & mask) == value)
Quoted 8 lines

drivers/char/tpm/tpm_crb.c struct tpm2_crb_smc { · line 131

Quoted 6 lines
drivers/char/tpm/tpm_crb.c +6 -0
+			return true;
+
+		usleep_range(50, 100);
+	} while (ktime_before(ktime_get(), stop));
+
+	return ((ioread32(reg) & mask) == value);

Not sure about the change of return statement. Is this worth of doing?

The sleep statement is followed directly by loop time comparison. It should take care for case when the change in the value happens in that time windows on the edge of the timeout. When I look at the traces I think we should maybe make that sleep little longer to reduce number of register access, but some more tests need to be done on multiple platforms.

Quoted 169 lines
Quoted 15 lines
drivers/char/tpm/tpm_crb.c +3 -1
+}
+
 /**
  * crb_go_idle - request tpm crb device to go the idle state
  *
@@ -128,7 +147,7 @@ struct tpm2_crb_smc {
  *
  * Return: 0 always
  */
-static int __maybe_unused crb_go_idle(struct device *dev, struct
 crb_priv *priv)
+static int crb_go_idle(struct device *dev, struct crb_priv *priv)
 {
 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
Quoted 1 line

(priv->sm ==

ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) || @@

Quoted 1 line

-136,30 +155,17 @@ static int __maybe_unused crb_go_idle(struct device

*dev, struct crb_priv *priv)

drivers/char/tpm/tpm_crb.c static bool crb_wait_for_reg_32(u32 __iomem *reg,

Quoted 38 lines
drivers/char/tpm/tpm_crb.c +7 -20
 		return 0;
 
 	iowrite32(CRB_CTRL_REQ_GO_IDLE, &priv->regs_t->ctrl_req);
-	/* we don't really care when this settles */
 
+	if (!crb_wait_for_reg_32(&priv->regs_t->ctrl_req,
+				 CRB_CTRL_REQ_GO_IDLE/* mask */,
+				 0, /* value */
+				 TPM2_TIMEOUT_C)) {
+		dev_warn(dev, "goIdle timed out\n");
+		return -ETIME;
+	}
 	return 0;
 }
 
-static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
-				unsigned long timeout)
-{
-	ktime_t start;
-	ktime_t stop;
-
-	start = ktime_get();
-	stop = ktime_add(start, ms_to_ktime(timeout));
-
-	do {
-		if ((ioread32(reg) & mask) == value)
-			return true;
-
-		usleep_range(50, 100);
-	} while (ktime_before(ktime_get(), stop));
-
-	return false;
-}
-
 /**
  * crb_cmd_ready - request tpm crb device to enter ready state
  *
@@ -175,8 +181,7 @@ static bool crb_wait_for_reg_32(u32 __iomem *reg,

u32 mask, u32 value,

drivers/char/tpm/tpm_crb.c static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,

Quoted 9 lines
drivers/char/tpm/tpm_crb.c +1 -2
  *
  * Return: 0 on success -ETIME on timeout;
  */
-static int __maybe_unused crb_cmd_ready(struct device *dev,
-					struct crb_priv *priv)
+static int crb_cmd_ready(struct device *dev, struct crb_priv *priv)
 {
 	if ((priv->sm == ACPI_TPM2_START_METHOD) ||
 	    (priv->sm ==

ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) || @@

Quoted 1 line

-195,11 +200,11 @@ static int __maybe_unused crb_cmd_ready(struct

device *dev,

drivers/char/tpm/tpm_crb.c static int crb_request_locality(struct tpm_chip

Quoted 15 lines
drivers/char/tpm/tpm_crb.c +3 -3
 	return 0;
 }
 
-static int crb_request_locality(struct tpm_chip *chip, int loc)
+static int __crb_request_locality(struct device *dev,
+				  struct crb_priv *priv, int loc)
 {
-	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
 	u32 value = CRB_LOC_STATE_LOC_ASSIGNED |
-		CRB_LOC_STATE_TPM_REG_VALID_STS;
+		    CRB_LOC_STATE_TPM_REG_VALID_STS;
 
 	if (!priv->regs_h)
 		return 0;
@@ -207,23 +212,36 @@ static int crb_request_locality(struct tpm_chip

*chip, int loc)

drivers/char/tpm/tpm_crb.c static int crb_request_locality(struct tpm_chip · line 211

Quoted 5 lines
drivers/char/tpm/tpm_crb.c +0 -1
 	iowrite32(CRB_LOC_CTRL_REQUEST_ACCESS, &priv->regs_h-
 loc_ctrl);
 	if (!crb_wait_for_reg_32(&priv->regs_h->loc_state, value, value,
 				 TPM2_TIMEOUT_C)) {
-		dev_warn(&chip->dev, "TPM_LOC_STATE_x.requestAccess

timed out\n");

drivers/char/tpm/tpm_crb.c static int crb_request_locality(struct tpm_chip · line 216

Quoted 1 line
drivers/char/tpm/tpm_crb.c +1 -0
+		dev_warn(dev, "TPM_LOC_STATE_x.requestAccess timed

out\n");

Quoted 18 lines
drivers/char/tpm/tpm_crb.c +5 -1
 		return -ETIME;
 	}
 
 	return 0;
 }
 
-static void crb_relinquish_locality(struct tpm_chip *chip, int loc)
+static int crb_request_locality(struct tpm_chip *chip, int loc)
 {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
 
+	return __crb_request_locality(&chip->dev, priv, loc); }
+
+static void __crb_relinquish_locality(struct device *dev,
+				      struct crb_priv *priv, int loc) {
 	if (!priv->regs_h)
 		return;
 

drivers/char/tpm/tpm_crb.c static int crb_request_locality(struct tpm_chip *chip, int loc)

Quoted 10 lines
drivers/char/tpm/tpm_crb.c +6 -0
 	iowrite32(CRB_LOC_CTRL_RELINQUISH, &priv->regs_h->loc_ctrl);  }
 
+static void crb_relinquish_locality(struct tpm_chip *chip, int loc) {
+	struct crb_priv *priv = dev_get_drvdata(&chip->dev);
+
+	__crb_relinquish_locality(&chip->dev, priv, loc); }
+
 static u8 crb_status(struct tpm_chip *chip)  {
 	struct crb_priv *priv = dev_get_drvdata(&chip->dev); @@ -475,6
+493,10 @@ static int crb_map_io(struct acpi_device *device, struct

crb_priv *priv,

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device

Quoted 21 lines
drivers/char/tpm/tpm_crb.c +6 -0
 			dev_warn(dev, FW_BUG "Bad ACPI memory layout");
 	}
 
+	ret = __crb_request_locality(dev, priv, 0);
+	if (ret)
+		return ret;
+
 	priv->regs_t = crb_map_res(dev, priv, &io_res, buf->control_address,
 				   sizeof(struct crb_regs_tail));
 	if (IS_ERR(priv->regs_t))
@@ -531,6 +553,8 @@ static int crb_map_io(struct acpi_device *device,
 struct crb_priv *priv,
 
 	crb_go_idle(dev, priv);
 
+	__crb_relinquish_locality(dev, priv, 0);
+
 	return ret;
 }
@@ -588,10 +612,14 @@ static int crb_acpi_add(struct acpi_device

*device)

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device

Quoted 16 lines
drivers/char/tpm/tpm_crb.c +5 -1
 	chip->acpi_dev_handle = device->handle;
 	chip->flags = TPM_CHIP_FLAG_TPM2;
 
-	rc  = crb_cmd_ready(dev, priv);
+	rc = __crb_request_locality(dev, priv, 0);
 	if (rc)
 		return rc;
 
+	rc  = crb_cmd_ready(dev, priv);
+	if (rc)
+		goto out;
+
 	pm_runtime_get_noresume(dev);
 	pm_runtime_set_active(dev);
 	pm_runtime_enable(dev);
@@ -601,12 +629,15 @@ static int crb_acpi_add(struct acpi_device

*device)

Quoted 7 lines
drivers/char/tpm/tpm_crb.c +1 -1
 		crb_go_idle(dev, priv);
 		pm_runtime_put_noidle(dev);
 		pm_runtime_disable(dev);
-		return rc;
+		goto out;
 	}
 
Quoted 4 lines

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device *device) · line 635

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +1 -1
-	pm_runtime_put(dev);
+	pm_runtime_put_sync(dev);

Change to put_sync is needed so that the idle handshake gets completed?

Right.

Quoted 8 lines
Quoted 8 lines
drivers/char/tpm/tpm_crb.c +4 -1
-	return 0;
+out:
+	__crb_relinquish_locality(dev, priv, 0);
+
+	return rc;
 }
 
Quoted 6 lines
Quoted 4 lines
drivers/char/tpm/tpm_crb.c +0 -1
 static int crb_acpi_remove(struct acpi_device *device)
--

2.14.3

/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 3/3] tpm: add longer timeouts for creation commands.

In reply to Jarkko Sakkinen
Quoted 12 lines

-----Original Message-----
From: Jarkko Sakkinen [mailto:jarkko.sakkinen at linux.intel.com]
Sent: Tuesday, January 23, 2018 15:12
To: Winkler, Tomas <[email protected]>
Cc: Jason Gunthorpe <[email protected]>; Usyskin, Alexander <[email protected]>; tpmdd-
devel at lists.sourceforge.net; linux-integrity at vger.kernel.org; linux-security- module at vger.kernel.org; linux-kernel at vger.kernel.org
Subject: Re: [PATCH 3/3] tpm: add longer timeouts for creation commands.

Quoted 8 lines
Quoted 5 lines · Tomas Winkler

TPM2_CC_Create(0x153) and TPM2_CC_CreatePrimary (0x131) involve generation of crypto keys which can be a computationally intensive task. The timeout is set to 3min.

From where are these numbers are derived from?

An Intel SW architect.

Quoted 4 lines

Can you send 1/3 and 2/3 as a separate patch set with a cover letter (with explanation) and this as a separate patch? This is unrelated to first two changes albeit might make sense.

Will do, though the patches were born to address the initialization issues we've encountered. 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 2/3] tpm: separate cmd_ready/go_idle from runtime_pm

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Jarkko Sakkinen
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.

Quoted 8 lines
Quoted 5 lines

Thank you.

LGTM

Jason, what do you think?

The PM stuff has been the source of confusion for a while, seems reasonable to get rid of it.

I'll test this ASAP.

/Jarkk
--
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
In reply to Winkler, Tomas
Quoted 6 lines · Tomas
Quoted 4 lines

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device *device) · line 635

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +1 -1
-	pm_runtime_put(dev);
+	pm_runtime_put_sync(dev);

Change to put_sync is needed so that the idle handshake gets completed?

Right.

Since we seem to agree that your change to get rid of the use of PM runtime should that be actually the first change in the patch set?

Or are you looking forward to get these backported to stable kernels?

/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
Quoted 7 lines · Tomas
Quoted 4 lines
Quoted 4 lines

drivers/char/tpm/tpm_crb.c static int crb_acpi_add(struct acpi_device *device) · line 635

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +1 -1
-	pm_runtime_put(dev);
+	pm_runtime_put_sync(dev);

Change to put_sync is needed so that the idle handshake gets

Quoted 8 lines

completed?

Quoted 2 lines

Right.

Since we seem to agree that your change to get rid of the use of PM runtime should that be actually the first change in the patch set?

Or are you looking forward to get these backported to stable kernels?

Yes, that was one of the reason behind for splitting this into two patches. So this first patch is the actual fix and second is the rather non user visible fix as runtime_pm should be enabled by default. But .. maybe both need to be backported.

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