Skip to contentSkip to conversation

additional TPM performance improvements

PATCH In review Nayna Jain 6 participants 29 messages Oct 17, 2017 – Nov 28, 2017

Subsystems (3)
lore
Share & tools
lore

Nayna Jain

author
#0 lore raw

v4 cover additional TPM performance improvements

After further discussions with the TCG Device Driver working group (ddwg), the following changes were made:

* Check for burstcount at least once to confirm the TPM is ready to accept the data. Similarly, query for the TPM Expect status as sanity check at the end.

* Make the sleep for status check in the loop less than 5msec.

* Make the sleep in the loop while querying for burstcount less than 5msec.

Below is the list of patches along with the performance improvements seen with a TPM 1.2 with an 8 byte burstcount for 1000 extends:

Patch |Improvement(time in sec)

tpm: ignore burstcount to improve tpm_tis | ~41 - ~14
send() performance.

tpm: reduce polling delay in tpm_tis_core | ~14 - ~9

tpm: modify tpm_msleep() function to have | ~9 - ~8
max range

Changelog v4:
* Include Jarkko's feedback on merging two patches reducing sleep time during polling to TPM.
* Include Jarkko's feedback to clean up the burstcount change patch and add comment for new #define.

Changelog v3:

* Include Jarkko's feedback to move wait_for_tpm_stat() from tpm-interface.c to respective driver files tpm_tis_core.c and xen-tpmfront.c.
* Remove use of module parameter ignore_burst_count and is now alwaysed ignored in tpm tis send() operation.
* Reduce sleep() time during polling in wait_for_tpm_stat() from 5msec to 1msec for both recv() and send() operation. Version 2 of the patches had it only for send() operation.

Changelog v2:

* Add module parameter to handle ignoring of burst count during tpm tis send() operation.
* Add improvements over sleep time to reduce delays.

Nayna Jain (4):
tpm: move wait_for_tpm_stat() to respective driver files
tpm: ignore burstcount to improve tpm_tis send() performance
tpm: reduce tpm polling delay in tpm_tis_core
tpm: use tpm_msleep() value as max delay

 drivers/char/tpm/tpm-interface.c |  60 ---------------------
 drivers/char/tpm/tpm.h           |   6 +--
 drivers/char/tpm/tpm_tis_core.c  | 110 +++++++++++++++++++++++++++++----------
 drivers/char/tpm/xen-tpmfront.c  |  60 +++++++++++++++++++++
 4 files changed, 144 insertions(+), 92 deletions(-)
Signature · 6 lines
2.13.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
author
#1 lore raw

PATCH v4 1/4 tpm: move wait_for_tpm_stat() to respective driver files

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

---
 drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
 drivers/char/tpm/tpm.h           |  2 --
 drivers/char/tpm/tpm_tis_core.c  | 60 ++++++++++++++++++++++++++++++++++++++++
 drivers/char/tpm/xen-tpmfront.c  | 60 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -60
index 1d6729be4cd6..313f7618d569 100644
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
 }
 EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
 #define TPM_ORD_SAVESTATE 152
 #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -2
index 2d5466a72e40..4fc83ac7abeb 100644
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
-		      wait_queue_head_t *queue, bool check_cancel);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
drivers/char/tpm/tpm_tis_core.c +60 -0

66 lines not on this page · raw message

drivers/char/tpm/xen-tpmfront.c +60 -0

66 lines not on this page · raw message

Signature · 6 lines
2.13.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
Reviewed-by Tested-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] tpm: move wait_for_tpm_stat() to respective driver files

Quoted 244 lines · Nayna Jain

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

---
 drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
 drivers/char/tpm/tpm.h           |  2 --
 drivers/char/tpm/tpm_tis_core.c  | 60 ++++++++++++++++++++++++++++++++++++++++
 drivers/char/tpm/xen-tpmfront.c  | 60 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm-interface.c +0 -60
index 1d6729be4cd6..313f7618d569 100644
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
 }
 EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
 #define TPM_ORD_SAVESTATE 152
 #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -2
index 2d5466a72e40..4fc83ac7abeb 100644
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
-		      wait_queue_head_t *queue, bool check_cancel);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
drivers/char/tpm/tpm_tis_core.c +60 -0
index 63bc6c3b949e..b33126a35694 100644
@@ -31,6 +31,66 @@
 #include "tpm.h"
 #include "tpm_tis_core.h"
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 /* Before we attempt to access the TPM we must see that the valid bit is set.
  * The specification says that this bit is 0 at reset and remains 0 until the
  * 'TPM has gone through its self test and initialization and has established
drivers/char/tpm/xen-tpmfront.c +60 -0
index 656e8af95d52..d20a0a9ded27 100644
@@ -39,6 +39,66 @@ enum status_bits {
 	VTPM_STATUS_CANCELED = 0x8,
 };
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 static u8 vtpm_status(struct tpm_chip *chip)
 {
 	struct tpm_private *priv = dev_get_drvdata(&chip->dev);
Signature · 1 line
2.13.3

This commit caused a compiler warning because freezer.h was not included to xen-tpmfront.c. I manually fixed it.

/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 v4 1/4] tpm: move wait_for_tpm_stat() to respective driver files

Quoted 250 lines · Nayna Jain
Quoted 5 lines

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

Quoted 7 lines
---
 drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
 drivers/char/tpm/tpm.h           |  2 --
 drivers/char/tpm/tpm_tis_core.c  | 60

++++++++++++++++++++++++++++++++++++++++

Quoted 1 line
 drivers/char/tpm/xen-tpmfront.c  | 60

++++++++++++++++++++++++++++++++++++++++

drivers/char/tpm/tpm-

Quoted 3 lines
 4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm- renamed from drivers/char/tpm/tpm-interface.c +0 -0

interface.c

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t

Quoted 4 lines

index 1d6729be4cd6..313f7618d569 100644

drivers/char/tpm/tpm-interface.c +0 -0
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t

buflen)

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -6
 }
 EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t buflen)

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +0 -10
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Quoted 52 lines
drivers/char/tpm/tpm-interface.c +0 -44
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
 #define TPM_ORD_SAVESTATE 152
 #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -0
index 2d5466a72e40..4fc83ac7abeb 100644

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip); · line 525

Quoted 1 line
drivers/char/tpm/tpm.h +0 -0
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32

ordinal);

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip);

Quoted 3 lines
drivers/char/tpm/tpm.h +0 -1
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Review

Quoted 5 lines
drivers/char/tpm/tpm.h +0 -1
-		      wait_queue_head_t *queue, bool check_cancel);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c

Quoted 58 lines

index 63bc6c3b949e..b33126a35694 100644

drivers/char/tpm/tpm_tis_core.c +51 -0
@@ -31,6 +31,66 @@
 #include "tpm.h"
 #include "tpm_tis_core.h"
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {

drivers/char/tpm/tpm_tis_core.c line 94

Quoted 10 lines
drivers/char/tpm/tpm_tis_core.c +9 -0
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 /* Before we attempt to access the TPM we must see that the valid bit is

set.

drivers/char/tpm/xen-

Quoted 3 lines
drivers/char/tpm/tpm_tis_core.c +0 -0
  * The specification says that this bit is 0 at reset and remains 0 until the
  * 'TPM has gone through its self test and initialization and has established
drivers/char/tpm/xen- renamed from drivers/char/tpm/xen-tpmfront.c +0 -0

tpmfront.c

Quoted 68 lines

index 656e8af95d52..d20a0a9ded27 100644

drivers/char/tpm/xen-tpmfront.c +60 -0
@@ -39,6 +39,66 @@ enum status_bits {
 	VTPM_STATUS_CANCELED = 0x8,
 };
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 static u8 vtpm_status(struct tpm_chip *chip)
Quoted 8 lines
Quoted 5 lines
drivers/char/tpm/xen-tpmfront.c +0 -1
 {
 	struct tpm_private *priv = dev_get_drvdata(&chip->dev);
--

2.13.3

This commit caused a compiler warning because freezer.h was not included to xen-tpmfront.c. I manually fixed it.

Yep, and checkpatch.pl still complains about some stuff (mostly whitespace) in both patch 1/4 and 2/4. When fixed patches are available, I can run them again through my automation.

Quoted 4 lines

/Jarkko

Alexander
--
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 v4 1/4] tpm: move wait_for_tpm_stat() to respective driver files

Quoted 259 lines · Alexander.Steffen at infineon.com
Quoted 250 lines · Nayna Jain
Quoted 5 lines

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

Quoted 7 lines
---
 drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
 drivers/char/tpm/tpm.h           |  2 --
 drivers/char/tpm/tpm_tis_core.c  | 60

++++++++++++++++++++++++++++++++++++++++

Quoted 1 line
 drivers/char/tpm/xen-tpmfront.c  | 60

++++++++++++++++++++++++++++++++++++++++

drivers/char/tpm/tpm-

Quoted 3 lines
 4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm- renamed from drivers/char/tpm/tpm-interface.c +0 -0

interface.c

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t

Quoted 4 lines

index 1d6729be4cd6..313f7618d569 100644

drivers/char/tpm/tpm-interface.c +0 -0
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t

buflen)

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -6
 }
 EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t buflen)

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +0 -10
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Quoted 52 lines
drivers/char/tpm/tpm-interface.c +0 -44
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
 #define TPM_ORD_SAVESTATE 152
 #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -0
index 2d5466a72e40..4fc83ac7abeb 100644

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip); · line 525

Quoted 1 line
drivers/char/tpm/tpm.h +0 -0
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32

ordinal);

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip);

Quoted 3 lines
drivers/char/tpm/tpm.h +0 -1
 int tpm_pm_suspend(struct device *dev);
 int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Review

Quoted 5 lines
drivers/char/tpm/tpm.h +0 -1
-		      wait_queue_head_t *queue, bool check_cancel);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c

Quoted 58 lines

index 63bc6c3b949e..b33126a35694 100644

drivers/char/tpm/tpm_tis_core.c +51 -0
@@ -31,6 +31,66 @@
 #include "tpm.h"
 #include "tpm_tis_core.h"
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {

drivers/char/tpm/tpm_tis_core.c line 94

Quoted 10 lines
drivers/char/tpm/tpm_tis_core.c +9 -0
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 /* Before we attempt to access the TPM we must see that the valid bit is

set.

drivers/char/tpm/xen-

Quoted 3 lines
drivers/char/tpm/tpm_tis_core.c +0 -0
  * The specification says that this bit is 0 at reset and remains 0 until the
  * 'TPM has gone through its self test and initialization and has established
drivers/char/tpm/xen- renamed from drivers/char/tpm/xen-tpmfront.c +0 -0

tpmfront.c

Quoted 68 lines

index 656e8af95d52..d20a0a9ded27 100644

drivers/char/tpm/xen-tpmfront.c +60 -0
@@ -39,6 +39,66 @@ enum status_bits {
 	VTPM_STATUS_CANCELED = 0x8,
 };
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
 static u8 vtpm_status(struct tpm_chip *chip)
Quoted 8 lines
Quoted 5 lines
drivers/char/tpm/xen-tpmfront.c +0 -1
 {
 	struct tpm_private *priv = dev_get_drvdata(&chip->dev);
--

2.13.3

This commit caused a compiler warning because freezer.h was not included to xen-tpmfront.c. I manually fixed it.

Quoted 4 lines

Yep, and checkpatch.pl still complains about some stuff (mostly whitespace) in both patch 1/4 and 2/4. When fixed patches are available, I can run them again through my automation. Alexander

I'll fix these manually (weird, I run sparse and checkpatch, must have done some mistake).

Thank you!

/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

author
#5 lore raw

Re: [PATCH v4 1/4] tpm: move wait_for_tpm_stat() to respective driver files

Quoted 258 lines · Jarkko Sakkinen
Quoted 257 lines · Alexander.Steffen at infineon.com
Quoted 257 lines · Nayna Jain
Quoted 5 lines

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

Quoted 7 lines
---
  drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
  drivers/char/tpm/tpm.h           |  2 --
  drivers/char/tpm/tpm_tis_core.c  | 60

++++++++++++++++++++++++++++++++++++++++

Quoted 1 line
  drivers/char/tpm/xen-tpmfront.c  | 60

++++++++++++++++++++++++++++++++++++++++

drivers/char/tpm/tpm-

Quoted 3 lines
  4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm- renamed from drivers/char/tpm/tpm-interface.c +0 -0

interface.c

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t

Quoted 4 lines

index 1d6729be4cd6..313f7618d569 100644

drivers/char/tpm/tpm-interface.c +0 -0
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t

buflen)

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -6
  }
  EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t buflen)

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +0 -10
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Quoted 52 lines
drivers/char/tpm/tpm-interface.c +0 -44
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
  #define TPM_ORD_SAVESTATE 152
  #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -0
index 2d5466a72e40..4fc83ac7abeb 100644

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip); · line 525

Quoted 1 line
drivers/char/tpm/tpm.h +0 -0
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
  unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32

ordinal);

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip);

Quoted 3 lines
drivers/char/tpm/tpm.h +0 -1
  int tpm_pm_suspend(struct device *dev);
  int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Review

Quoted 5 lines
drivers/char/tpm/tpm.h +0 -1
-		      wait_queue_head_t *queue, bool check_cancel);
 
  static inline void tpm_msleep(unsigned int delay_msec)
  {
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c

Quoted 58 lines

index 63bc6c3b949e..b33126a35694 100644

drivers/char/tpm/tpm_tis_core.c +51 -0
@@ -31,6 +31,66 @@
  #include "tpm.h"
  #include "tpm_tis_core.h"
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {

drivers/char/tpm/tpm_tis_core.c line 94

Quoted 10 lines
drivers/char/tpm/tpm_tis_core.c +9 -0
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
  /* Before we attempt to access the TPM we must see that the valid bit is

set.

drivers/char/tpm/xen-

Quoted 3 lines
drivers/char/tpm/tpm_tis_core.c +0 -0
   * The specification says that this bit is 0 at reset and remains 0 until the
   * 'TPM has gone through its self test and initialization and has established
drivers/char/tpm/xen- renamed from drivers/char/tpm/xen-tpmfront.c +0 -0

tpmfront.c

Quoted 71 lines

index 656e8af95d52..d20a0a9ded27 100644

drivers/char/tpm/xen-tpmfront.c +60 -1
@@ -39,6 +39,66 @@ enum status_bits {
  	VTPM_STATUS_CANCELED = 0x8,
  };
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
  static u8 vtpm_status(struct tpm_chip *chip)
  {
  	struct tpm_private *priv = dev_get_drvdata(&chip->dev);
--
Quoted 2 lines

2.13.3

This commit caused a compiler warning because freezer.h was not included to xen-tpmfront.c. I manually fixed it.

Quoted 4 lines
Quoted 4 lines

Yep, and checkpatch.pl still complains about some stuff (mostly whitespace) in both patch 1/4 and 2/4. When fixed patches are available, I can run them again through my automation. Alexander

Sorry for delay in response.. I was on vacation for last few days. Thanks for informing. I am wondering these didn't show up when I ran checkpatch.pl.

Quoted 2 lines

I'll fix these manually (weird, I run sparse and checkpatch, must have done some mistake).

Thanks Jarkko.

Thanks & Regards,
??? - Nayna

Quoted 4 lines

Thank you!

/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 v4 1/4] tpm: move wait_for_tpm_stat() to respective driver files

Quoted 261 lines · Nayna Jain
Quoted 258 lines · Jarkko Sakkinen
Quoted 257 lines · Alexander.Steffen at infineon.com
Quoted 257 lines · Nayna Jain
Quoted 5 lines

The function wait_for_tpm_stat() is currently defined in tpm-interface file. It is a hardware specific function used only by tpm_tis and xen-tpmfront, so it is removed from tpm-interface.c and defined in respective driver files.

Quoted 7 lines
---
  drivers/char/tpm/tpm-interface.c | 60 ----------------------------------------
  drivers/char/tpm/tpm.h           |  2 --
  drivers/char/tpm/tpm_tis_core.c  | 60

++++++++++++++++++++++++++++++++++++++++

Quoted 1 line
  drivers/char/tpm/xen-tpmfront.c  | 60

++++++++++++++++++++++++++++++++++++++++

drivers/char/tpm/tpm-

Quoted 3 lines
  4 files changed, 120 insertions(+), 62 deletions(-)
drivers/char/tpm/tpm- renamed from drivers/char/tpm/tpm-interface.c +0 -0

interface.c

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t

Quoted 4 lines

index 1d6729be4cd6..313f7618d569 100644

drivers/char/tpm/tpm-interface.c +0 -0
@@ -1035,66 +1035,6 @@ int tpm_send(u32 chip_num, void *cmd, size_t

buflen)

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -6
  }
  EXPORT_SYMBOL_GPL(tpm_send);
 
-static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
-					bool check_cancel, bool *canceled)
-{
-	u8 status = chip->ops->status(chip);
-
-	*canceled = false;

drivers/char/tpm/tpm-interface.c int tpm_send(u32 chip_num, void *cmd, size_t buflen)

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +0 -10
-	if ((status & mask) == mask)
-		return true;
-	if (check_cancel && chip->ops->req_canceled(chip, status)) {
-		*canceled = true;
-		return true;
-	}
-	return false;
-}
-
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Quoted 52 lines
drivers/char/tpm/tpm-interface.c +0 -44
-		      wait_queue_head_t *queue, bool check_cancel)
-{
-	unsigned long stop;
-	long rc;
-	u8 status;
-	bool canceled = false;
-
-	/* check current status */
-	status = chip->ops->status(chip);
-	if ((status & mask) == mask)
-		return 0;
-
-	stop = jiffies + timeout;
-
-	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
-again:
-		timeout = stop - jiffies;
-		if ((long)timeout <= 0)
-			return -ETIME;
-		rc = wait_event_interruptible_timeout(*queue,
-			wait_for_tpm_stat_cond(chip, mask, check_cancel,
-					       &canceled),
-			timeout);
-		if (rc > 0) {
-			if (canceled)
-				return -ECANCELED;
-			return 0;
-		}
-		if (rc == -ERESTARTSYS && freezing(current)) {
-			clear_thread_flag(TIF_SIGPENDING);
-			goto again;
-		}
-	} else {
-		do {
-			tpm_msleep(TPM_TIMEOUT);
-			status = chip->ops->status(chip);
-			if ((status & mask) == mask)
-				return 0;
-		} while (time_before(jiffies, stop));
-	}
-	return -ETIME;
-}
-EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
-
  #define TPM_ORD_SAVESTATE 152
  #define SAVESTATE_RESULT_SIZE 10
 
drivers/char/tpm/tpm.h +0 -0
index 2d5466a72e40..4fc83ac7abeb 100644

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip); · line 525

Quoted 1 line
drivers/char/tpm/tpm.h +0 -0
@@ -525,8 +525,6 @@ int tpm_do_selftest(struct tpm_chip *chip);
  unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32

ordinal);

drivers/char/tpm/tpm.h int tpm_do_selftest(struct tpm_chip *chip);

Quoted 3 lines
drivers/char/tpm/tpm.h +0 -1
  int tpm_pm_suspend(struct device *dev);
  int tpm_pm_resume(struct device *dev);
-int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long

timeout,

Review

Quoted 5 lines
drivers/char/tpm/tpm.h +0 -1
-		      wait_queue_head_t *queue, bool check_cancel);
 
  static inline void tpm_msleep(unsigned int delay_msec)
  {
(quoted diff) +0 -0

b/drivers/char/tpm/tpm_tis_core.c

Quoted 58 lines

index 63bc6c3b949e..b33126a35694 100644

drivers/char/tpm/tpm_tis_core.c +51 -0
@@ -31,6 +31,66 @@
  #include "tpm.h"
  #include "tpm_tis_core.h"
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {

drivers/char/tpm/tpm_tis_core.c line 94

Quoted 10 lines
drivers/char/tpm/tpm_tis_core.c +9 -0
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
  /* Before we attempt to access the TPM we must see that the valid bit is

set.

drivers/char/tpm/xen-

Quoted 3 lines
drivers/char/tpm/tpm_tis_core.c +0 -0
   * The specification says that this bit is 0 at reset and remains 0 until the
   * 'TPM has gone through its self test and initialization and has established
drivers/char/tpm/xen- renamed from drivers/char/tpm/xen-tpmfront.c +0 -0

tpmfront.c

Quoted 71 lines

index 656e8af95d52..d20a0a9ded27 100644

drivers/char/tpm/xen-tpmfront.c +60 -1
@@ -39,6 +39,66 @@ enum status_bits {
  	VTPM_STATUS_CANCELED = 0x8,
  };
 
+static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
+					bool check_cancel, bool *canceled)
+{
+	u8 status = chip->ops->status(chip);
+
+	*canceled = false;
+	if ((status & mask) == mask)
+		return true;
+	if (check_cancel && chip->ops->req_canceled(chip, status)) {
+		*canceled = true;
+		return true;
+	}
+	return false;
+}
+
+static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
+		unsigned long timeout, wait_queue_head_t *queue,
+		bool check_cancel)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+	bool canceled = false;
+
+	/* check current status */
+	status = chip->ops->status(chip);
+	if ((status & mask) == mask)
+		return 0;
+
+	stop = jiffies + timeout;
+
+	if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+		timeout = stop - jiffies;
+		if ((long)timeout <= 0)
+			return -ETIME;
+		rc = wait_event_interruptible_timeout(*queue,
+			wait_for_tpm_stat_cond(chip, mask, check_cancel,
+					       &canceled),
+			timeout);
+		if (rc > 0) {
+			if (canceled)
+				return -ECANCELED;
+			return 0;
+		}
+		if (rc == -ERESTARTSYS && freezing(current)) {
+			clear_thread_flag(TIF_SIGPENDING);
+			goto again;
+		}
+	} else {
+		do {
+			tpm_msleep(TPM_TIMEOUT);
+			status = chip->ops->status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+}
+
  static u8 vtpm_status(struct tpm_chip *chip)
  {
  	struct tpm_private *priv = dev_get_drvdata(&chip->dev);
--
Quoted 2 lines

2.13.3

This commit caused a compiler warning because freezer.h was not included to xen-tpmfront.c. I manually fixed it.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Yep, and checkpatch.pl still complains about some stuff (mostly whitespace) in both patch 1/4 and 2/4. When fixed patches are available, I can run them again through my automation. Alexander

Sorry for delay in response.. I was on vacation for last few days. Thanks for informing. I am wondering these didn't show up when I ran checkpatch.pl.

I also did run checkpatch.pl. Haven't had time to recheck yet but I will do that since there's always chance for human error.

/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

author
#7 lore raw

PATCH v4 2/4 tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

---
 drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +15 -27
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int rc, status, burstcnt;
-	size_t count = 0;
 	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
 	status = tpm_tis_status(chip);
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 		}
 	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
 	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
 	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-1]);
 	if (rc < 0)
 		goto out_err;
 
Signature · 6 lines
2.13.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
Reviewed-by on v4 2/4
#8 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Quoted 8 lines · Nayna Jain

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

Quoted 7 lines

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

/Jarkko

Quoted 70 lines
---
 drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +15 -27
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int rc, status, burstcnt;
-	size_t count = 0;
 	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
 	status = tpm_tis_status(chip);
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 		}
 	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
 	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
 	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-1]);
 	if (rc < 0)
 		goto out_err;
 
Signature · 1 line
2.13.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

Tested-by on v4 2/4
#9 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain
Quoted 85 lines · Nayna Jain

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

---
 drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +15 -27
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int rc, status, burstcnt;
-	size_t count = 0;
 	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
 	status = tpm_tis_status(chip);
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
 		}
 	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
 	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
 	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-1]);
 	if (rc < 0)
 		goto out_err;
 
Signature · 1 line
2.13.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

#10 lore raw

RE: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain
Quoted 91 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

---
 drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++----------------------
----
 1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -1
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int rc, status, burstcnt;
-	size_t count = 0;
 	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
drivers/char/tpm/tpm_tis_core.c +14 -26
 	status = tpm_tis_status(chip);
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
 		}
 	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 >locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
 >timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
 	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
 	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-

1]);

drivers/char/tpm/tpm_tis_core.c +0 -1
 	if (rc < 0)
 		goto out_err;
 
--
Quoted 1 line

2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to wait until November, since I am going to be in Prague next week.

Alexander
--
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 v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Quoted 6 lines · Alexander.Steffen at infineon.com

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to wait until November, since I am going to be in Prague next week.

I have a guess as to the cause of the failure. Would it be possible for you to test it?

1 - My guess is that EIO is coming from here:

static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
...

	/* write last byte */
	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
	if (rc < 0)
		goto out_err;

	if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
				&priv->int_queue, false) < 0) {
		rc = -ETIME;
		goto out_err;
	}
	status = tpm_tis_status(chip);
	if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
		rc = -EIO;
		goto out_err;
	}

...

Can you verify that this is the cause.

2 - If that's the cause, I believe that there is a latent bug. Expect is not guaranteed to become false immediately. It only occurs after the TPM firmware has emptied the FIFO. Thus, the tpm_tis_status() really should be something like "wait_for_tpm_expect_false()", with a sleep loop.

This missing wait has been in the code for a while. If may just surface now because the patch causes data to be written faster, and thus it takes longer for the TPM to empty the FIFO and clear Expect.

It also makes sense that it would occur more often on long commands.

--
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

#12 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to [email protected]
Quoted 99 lines · Alexander.Steffen at infineon.com
Quoted 91 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

---
 drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++----------------------
----
 1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -1
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int rc, status, burstcnt;
-	size_t count = 0;
 	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
drivers/char/tpm/tpm_tis_core.c +14 -26
 	status = tpm_tis_status(chip);
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
 		}
 	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 >locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
 >timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
 	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
 	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-

1]);

drivers/char/tpm/tpm_tis_core.c +0 -1
 	if (rc < 0)
 		goto out_err;
 
--
Quoted 1 line

2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to wait until November, since I am going to be in Prague next week.

Quoted 1 line

Alexander

For the time being I'll drop this patch from the next branch and keep it in the master branch.

/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

author
#13 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to [email protected]
Quoted 90 lines · Alexander.Steffen at infineon.com
Quoted 90 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

conjunction with the TPM Device Driver work group.

---
  drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++----------------------
----
  1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -1
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
  {
  	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  	int rc, status, burstcnt;
-	size_t count = 0;
  	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
  	status = tpm_tis_status(chip);
drivers/char/tpm/tpm_tis_core.c +14 -26
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip *chip,
 u8 *buf, size_t len)
  		}
  	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 > locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
 > timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
  	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
  	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-

1]);
if (rc < 0)
goto out_err;

Quoted 3 lines
Quoted 2 lines

--
2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to wait until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any further analysis ?

Thanks & Regards,
?????? - Nayna

Quoted 2 lines

Alexander

--
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

#14 lore raw

RE: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Quoted 97 lines · Alexander.Steffen at infineon.com
Quoted 11 lines
Quoted 11 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

Quoted 6 lines
Quoted 6 lines

conjunction with the TPM Device Driver work group.

---
  drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++------------------

----

Quoted 9 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip

Quoted 9 lines

----

  1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -0
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip

*chip,

Quoted 9 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip

Quoted 9 lines
drivers/char/tpm/tpm_tis_core.c +0 -1
 u8 *buf, size_t len)
  {
  	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  	int rc, status, burstcnt;
-	size_t count = 0;
  	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
  	status = tpm_tis_status(chip);
drivers/char/tpm/tpm_tis_core.c +0 -0
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip

*chip,

Quoted 55 lines
Quoted 55 lines
drivers/char/tpm/tpm_tis_core.c +14 -26
 u8 *buf, size_t len)
  		}
  	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 > locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
 > timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
  	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
  	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-

1]);
if (rc < 0)
goto out_err;

Quoted 3 lines
Quoted 2 lines

--
2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to

Quoted 6 lines

send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to wait until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any further analysis ?

I am working on that now. Ken's suggestion seems reasonable, so I am going to test whether correctly waiting for the flags to change fixes the problem. If it does, I'll send the patches.

Alexander
--
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

#15 lore raw

RE: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain
Quoted 101 lines
Quoted 12 lines · Alexander.Steffen at infineon.com
Quoted 11 lines
Quoted 11 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

conjunction with the TPM Device Driver work group.

---
  drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++----------------

--

Quoted 76 lines

----

Quoted 9 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip

Quoted 9 lines

----

  1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -0
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip

*chip,

Quoted 9 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip

Quoted 9 lines
drivers/char/tpm/tpm_tis_core.c +0 -1
 u8 *buf, size_t len)
  {
  	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  	int rc, status, burstcnt;
-	size_t count = 0;
  	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
  	status = tpm_tis_status(chip);
drivers/char/tpm/tpm_tis_core.c +0 -0
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct tpm_chip

*chip,

Quoted 55 lines
Quoted 55 lines
drivers/char/tpm/tpm_tis_core.c +14 -26
 u8 *buf, size_t len)
  		}
  	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read burstcount\n");
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 > locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
 > timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
  	}
 
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
  	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[len-

1]);
if (rc < 0)
goto out_err;

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

--
2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to

send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to

wait

Quoted 8 lines
Quoted 4 lines

until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any further analysis ?

I am working on that now. Ken's suggestion seems reasonable, so I am going to test whether correctly waiting for the flags to change fixes the problem. If it does, I'll send the patches.

Sorry for the delay, I had to take care of some device tree changes in v4.14 that broke my ARM test machines.

I've implemented some patches that fix the issue that Ken pointed out and rebased your patch 2/4 ("ignore burstcount") on top. While doing this I noticed that your original patch does not, as the commit message says, write all the bytes at once, but still unnecessarily splits all commands into at least two transfers (as did the original code). I've fixed this as well in my patches, so that all bytes are indeed sent in a single call, without special handling for the last byte. This should speed up things further, especially for small commands and drivers like tpm_tis_spi, where writing a single byte translates into additional SPI transfers.

Unfortunately, even with those changes the problem persists. But I've got more detailed logs now and will try to understand and hopefully fix the issue. I'll follow up with more details and/or patches once I know more.

Alexander
--
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

#16 lore raw

RE: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain
Quoted 131 lines
Quoted 12 lines
Quoted 12 lines · Alexander.Steffen at infineon.com
Quoted 11 lines
Quoted 11 lines

The TPM burstcount status indicates the number of bytes that can be sent to the TPM without causing bus wait states. Effectively, it is the number of empty bytes in the command FIFO.

This patch optimizes the tpm_tis_send_data() function by checking the burstcount only once. And if the burstcount is valid, it writes all the bytes at once, permitting wait state.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~41sec to ~14sec.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

conjunction with the TPM Device Driver work group.

---
  drivers/char/tpm/tpm_tis_core.c | 42 +++++++++++++++--------------

--

Quoted 19 lines

--

Quoted 18 lines

----

Quoted 9 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip

Quoted 9 lines

----

  1 file changed, 15 insertions(+), 27 deletions(-)
drivers/char/tpm/tpm_tis_core.c +0 -0
index b33126a35694..993328ae988c 100644
@@ -316,7 +316,6 @@ static int tpm_tis_send_data(struct tpm_chip

*chip,

Quoted 7 lines
Quoted 7 lines
drivers/char/tpm/tpm_tis_core.c +0 -1
 u8 *buf, size_t len)
  {
  	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
  	int rc, status, burstcnt;
-	size_t count = 0;
  	bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct

Quoted 2 lines
  	status = tpm_tis_status(chip);
drivers/char/tpm/tpm_tis_core.c +0 -0
@@ -330,35 +329,24 @@ static int tpm_tis_send_data(struct

tpm_chip

Quoted 9 lines
Quoted 9 lines

*chip,

Quoted 8 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)

Quoted 8 lines
drivers/char/tpm/tpm_tis_core.c +0 -4
 u8 *buf, size_t len)
  		}
  	}
 
-	while (count < len - 1) {
-		burstcnt = get_burstcount(chip);
-		if (burstcnt < 0) {
-			dev_err(&chip->dev, "Unable to read

burstcount\n");

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/char/tpm/tpm_tis_core.c +0 -12
-			rc = burstcnt;
-			goto out_err;
-		}
-		burstcnt = min_t(int, burstcnt, len - count - 1);
-		rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-
 > locality),
-					 burstcnt, buf + count);
-		if (rc < 0)
-			goto out_err;
-
-		count += burstcnt;
-
-		if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip-
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)

Quoted 7 lines
drivers/char/tpm/tpm_tis_core.c +0 -6
 > timeout_c,
-					&priv->int_queue, false) < 0) {
-			rc = -ETIME;
-			goto out_err;
-		}
-		status = tpm_tis_status(chip);
-		if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0)

{

Quoted 16 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/char/tpm/tpm_tis_core.c +9 -3
-			rc = -EIO;
-			goto out_err;
-		}
+	/*
+	 * Get the initial burstcount to ensure TPM is ready to
+	 * accept data.
+	 */
+	burstcnt = get_burstcount(chip);
+	if (burstcnt < 0) {
+		dev_err(&chip->dev, "Unable to read burstcount\n");
+		rc = burstcnt;
+		goto out_err;
  	}
 
Quoted 1 line
Quoted 1 line

drivers/char/tpm/tpm_tis_core.c static int tpm_tis_send_data(struct · line 346

Quoted 1 line
drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv-

locality),

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/char/tpm/tpm_tis_core.c

Quoted 8 lines
drivers/char/tpm/tpm_tis_core.c +4 -1
+			len - 1, buf);
+	if (rc < 0)
+		goto out_err;
+
  	/* write last byte */
-	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[count]);

drivers/char/tpm/tpm_tis_core.c +1 -0
+	rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality),

buf[len-

Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 6 lines

1]);
if (rc < 0)
goto out_err;

--
2.13.3

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to

send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to

Quoted 7 lines

wait

Quoted 4 lines

until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any further analysis ?

I am working on that now. Ken's suggestion seems reasonable, so I am

going

Quoted 1 line

to test whether correctly waiting for the flags to change fixes the problem.

If

Quoted 1 line

it does, I'll send the patches.

Sorry for the delay, I had to take care of some device tree changes in v4.14 that broke my ARM test machines.

I've implemented some patches that fix the issue that Ken pointed out and rebased your patch 2/4 ("ignore burstcount") on top. While doing this I noticed that your original patch does not, as the commit message says, write all the bytes at once, but still unnecessarily splits all commands into at least two transfers (as did the original code). I've fixed this as well in my patches, so that all bytes are indeed sent in a single call, without special handling for the last byte. This should speed up things further, especially for small commands and drivers like tpm_tis_spi, where writing a single byte translates into additional SPI transfers.

Quoted 3 lines

Unfortunately, even with those changes the problem persists. But I've got more detailed logs now and will try to understand and hopefully fix the issue. I'll follow up with more details and/or patches once I know more.

Okay, so the problem seems to be that at some point the TPM starts inserting wait states for the FIFO access. The driver tries to handle this, but fails since even the 50 retries that are currently used do not seem to be enough. Adding small (millisecond) delays between the attempts did not help so far.

Is there any limit in the specification for how many wait states the TPM may generate or for how long it may do so? I could not find anything, but we need to use something there to prevent a faulty TPM from blocking the kernel forever.

Alexander
--
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

author
#17 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Quoted 18 lines · Alexander.Steffen at infineon.com
Quoted 18 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying to

send large amounts of data, e.g. with TPM2_Hash, and subsequent tests seem to take an unusual amount of time. More analysis probably has to

wait

Quoted 4 lines

until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any further analysis ?

I am working on that now. Ken's suggestion seems reasonable, so I am

going

Quoted 1 line

to test whether correctly waiting for the flags to change fixes the problem.

If

Quoted 1 line

it does, I'll send the patches.

Sorry for the delay, I had to take care of some device tree changes in v4.14 that broke my ARM test machines.

Quoted 9 lines
Quoted 9 lines

I've implemented some patches that fix the issue that Ken pointed out and rebased your patch 2/4 ("ignore burstcount") on top. While doing this I noticed that your original patch does not, as the commit message says, write all the bytes at once, but still unnecessarily splits all commands into at least two transfers (as did the original code). I've fixed this as well in my patches, so that all bytes are indeed sent in a single call, without special handling for the last byte. This should speed up things further, especially for small commands and drivers like tpm_tis_spi, where writing a single byte translates into additional SPI transfers.

Thanks Alex, for digging into.

Yeah, you are right, the first version of this patch sent all the bytes together, but after hearing ddwg inputs, i.e. "The last byte was introduced for error checking purposes (history).", I reverted back to original to be safe.

It seems that the last byte was sent from the beginning (27084ef [PATCH] tpm: driver for next generation TPM chips,), does anyone remember the reason ?

Quoted 9 lines
Quoted 4 lines

Unfortunately, even with those changes the problem persists. But I've got more detailed logs now and will try to understand and hopefully fix the issue. I'll follow up with more details and/or patches once I know more.

Okay, so the problem seems to be that at some point the TPM starts inserting wait states for the FIFO access. The driver tries to handle this, but fails since even the 50 retries that are currently used do not seem to be enough. Adding small (millisecond) delays between the attempts did not help so far.

Is there any limit in the specification for how many wait states the TPM may generate or for how long it may do so? I could not find anything, but we need to use something there to prevent a faulty TPM from blocking the kernel forever.

I have been thinking on this, so was wondering:

1. As you said the problem started while sending large amounts of data for TPM2_Hash, how large is "large" ? I mean did it work for some specific large values before failing.
2. Are these wait states limited to SPI, or does it happen on LPC as well?

Thanks & Regards,
- Nayna

Quoted 2 lines

Alexander

--
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

#18 lore raw

RE: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Quoted 43 lines
Quoted 1 line · Alexander.Steffen at infineon.com
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

This seems to fail reliably with my SPI TPM 2.0. I get EIO when trying

to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

send large amounts of data, e.g. with TPM2_Hash, and subsequent

tests

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

seem to take an unusual amount of time. More analysis probably has

to

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

wait

Quoted 3 lines

until November, since I am going to be in Prague next week.

Thanks Alex for testing these.. Did you get the chance to do any

further

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

analysis ?

I am working on that now. Ken's suggestion seems reasonable, so I am

going

Quoted 1 line

to test whether correctly waiting for the flags to change fixes the

problem.

Quoted 4 lines
Quoted 4 lines

If

Quoted 1 line

it does, I'll send the patches.

Sorry for the delay, I had to take care of some device tree changes in

v4.14

Quoted 3 lines
Quoted 3 lines

that broke my ARM test machines.

I've implemented some patches that fix the issue that Ken pointed out

and

Quoted 2 lines
Quoted 2 lines

rebased your patch 2/4 ("ignore burstcount") on top. While doing this I noticed that your original patch does not, as the commit message says,

write

Quoted 1 line
Quoted 1 line

all the bytes at once, but still unnecessarily splits all commands into at

least

Quoted 1 line
Quoted 1 line

two transfers (as did the original code). I've fixed this as well in my

patches,

Quoted 1 line
Quoted 1 line

so that all bytes are indeed sent in a single call, without special handling

for

Quoted 3 lines
Quoted 3 lines

the last byte. This should speed up things further, especially for small commands and drivers like tpm_tis_spi, where writing a single byte translates into additional SPI transfers.

Thanks Alex, for digging into.

Quoted 8 lines

Yeah, you are right, the first version of this patch sent all the bytes together, but after hearing ddwg inputs,
i.e. "The last byte was introduced for error checking purposes (history).", I reverted back to original to be safe.

It seems that the last byte was sent from the beginning (27084ef [PATCH]
tpm: driver for next generation TPM chips,),
does anyone remember the reason ?

The intention seems to be to make extra sure that the TPM has correctly understood the command by observing the Expect flag flipping from 1 to 0 when writing the last byte.

But following Ken's arguments, this does not work as intended, because the Expect flag will change not when writing the last byte to the FIFO, but when the TPM reads the last byte from the FIFO. Since there is no "FIFO empty" indication, just observing the Expect flag to be 1 before writing the last byte, cannot reliably tell us anything (there might be enough data left in the FIFO for the Expect flag to flip to 0 without writing the last byte).

Also, I'd argue that this check is not necessary, because if the Expect flag is 0 after all bytes have been written to the FIFO, then the TPM has correctly received the command and is ready to execute it. According to TIS/PTP the TPM is required to throw away all extra bytes that were not announced in the header, and in addition the kernel driver already ensures not to send more data. That are enough safeguards, I'd say.

Quoted 20 lines
Quoted 3 lines
Quoted 3 lines

Unfortunately, even with those changes the problem persists. But I've

got

Quoted 1 line
Quoted 1 line

more detailed logs now and will try to understand and hopefully fix the

issue.

Quoted 3 lines
Quoted 1 line

I'll follow up with more details and/or patches once I know more.

Okay, so the problem seems to be that at some point the TPM starts

inserting wait states for the FIFO access. The driver tries to handle this, but fails since even the 50 retries that are currently used do not seem to be enough. Adding small (millisecond) delays between the attempts did not help so far.

Quoted 2 lines

Is there any limit in the specification for how many wait states the TPM may

generate or for how long it may do so? I could not find anything, but we need to use something there to prevent a faulty TPM from blocking the kernel forever.

Quoted 1 line
Quoted 5 lines

I have been thinking on this, so was wondering:

1. As you said the problem started while sending large amounts of data for TPM2_Hash, how large is "large" ? I mean did it work for some specific large values before failing.

Around 1k of data (the exact values are chosen randomly, and it failed many times), but I did not try to find a specific boundary. The interesting thing was that for this long command all SPI frames with the maximum payload of 64 bytes were accepted without wait states, but the last frame (with less than 64 bytes) caused the wait states.

Quoted 1 line

2. Are these wait states limited to SPI, or does it happen on LPC as well?

I do not know for LPC because there the wait states are handled in hardware and I cannot trace the LPC signals.

Quoted 7 lines

Thanks & Regards,
- Nayna

Quoted 2 lines

Alexander

--
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

#19 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Nayna Jain
Quoted 4 lines · Nayna Jain

Yeah, you are right, the first version of this patch sent all the bytes together, but after hearing ddwg inputs, i.e. "The last byte was introduced for error checking purposes (history).", I reverted back to original to be safe.

What does that mean ie error checking purposes?

Quoted 3 lines

It seems that the last byte was sent from the beginning (27084ef [PATCH] tpm: driver for next generation TPM chips,), does anyone remember the reason ?

Sent from the beginning?

/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

#20 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

[Cc'ing Dave and Leendeert]

Hi Jarkko,

Quoted 5 lines
Quoted 3 lines

It seems that the last byte was sent from the beginning (27084ef [PATCH] tpm: driver for next generation TPM chips,), does anyone remember the reason ?

Sent from the beginning?

I went through the commit logs to see if any of the patch descriptions have an explanation for sending the last byte separately. Based on commit 27084efee0c3 "[PATCH] tpm: driver for next generation TPM chips", it seems it's been there since the beginning.

Dave, Leendert, Do either of you remember the reason for tpm_tis_send_data() sending the last byte separately?

thanks,

Mimi

--
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

#21 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Hmm, this is almost 20 years old code (

I think the original code did a burst write and didn't check for error conditions until the very last byte write. I seem to remember that there was some text in the original standard to that effect (this may have gone back as far as IBM's ESS spec).

The current code does check for error conditions after each write byte(s) so I don't think there is any reason for this anymore. Changing the while condition to count < len and setting burstcnt = min_t(int, burstcnt, len - count) and remove the tpm_tis_write8/wait_for_tpm_stat/tpm_tis_status clauses after the while loop should be sufficient.

Leendert

???On 11/26/17, 8:37 AM, "Mimi Zohar" <[email protected]> wrote:

[Cc'ing Dave and Leendeert]

Hi Jarkko,

> > It seems that the last byte was sent from the beginning (27084ef > > [PATCH] tpm: driver for next generation TPM chips,), does anyone > > remember the reason ?
>
> Sent from the beginning?

I went through the commit logs to see if any of the patch descriptions have an explanation for sending the last byte separately. Based on commit 27084efee0c3 "[PATCH] tpm: driver for next generation TPM chips", it seems it's been there since the beginning.

Dave, Leendert, Do either of you remember the reason for tpm_tis_send_data() sending the last byte separately?

thanks,

Mimi

????{.n?+???????+%?????????w??{.n?+????{??????????v?^?)????w*jg??????????j????G?????? ???j:+v???w?j?m????????w?????f???h?????????

#22 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

Hi, Leendert!

Quoted 6 lines · Leendert van Doorn

Hmm, this is almost 20 years old code (

I think the original code did a burst write and didn't check for error conditions until the very last byte write. I seem to remember that there was some text in the original standard to that effect (this may have gone back as far as IBM's ESS spec).

I really appreciate your responding with your recollections.

Quoted 6 lines

The current code does check for error conditions after each write byte(s) so I don't think there is any reason for this anymore. Changing the while condition to count < len and setting burstcnt = min_t(int, burstcnt, len - count) and remove the tpm_tis_write8/wait_for_tpm_stat/tpm_tis_status clauses after the while loop should be sufficient.

The context for my question was about removing the while loop and sending all of the data at once, relying on the bus wait states. ?Before making the change, I wanted to make sure there wasn't anything special about the last byte.

thanks,

Mimi

--
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

#23 lore raw

Re: [PATCH v4 2/4] tpm: ignore burstcount to improve tpm_tis send() performance

In reply to Leendert van Doorn
Quoted 7 lines · Leendert van Doorn

Hmm, this is almost 20 years old code (

I think the original code did a burst write and didn't check for error conditions until the very last byte write. I seem to remember that there was some text in the original standard to that effect (this may have gone back as far as IBM's ESS spec).

Quoted 6 lines

The current code does check for error conditions after each write byte(s) so I don't think there is any reason for this anymore. Changing the while condition to count < len and setting burstcnt = min_t(int, burstcnt, len - count) and remove the tpm_tis_write8/wait_for_tpm_stat/tpm_tis_status clauses after the while loop should be sufficient.

Thank you for sharing this!

Quoted 1 line

Leendert

/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

author
#24 lore raw

PATCH v4 3/4 tpm: reduce tpm polling delay in tpm_tis_core

In reply to Nayna Jain

The existing wait_for_tpm_stat() polls for the chip status after 5msec sleep. As per TCG ddwg input, it is expected that tpm might return status in few usec. So, reducing the delay in polling to 1msec.
Similarly, get_burstcount() function sleeps for 5msec before retrying for next query to burstcount in a loop. If it takes lesser time for TPM to return, this 5msec delay is longer than necessary.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~14sec to ~9sec.

---
 drivers/char/tpm/tpm_tis_core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_tis_core.c +8 -2
index 993328ae988c..8b55fdaa67f2 100644
@@ -31,6 +31,12 @@
 #include "tpm.h"
 #include "tpm_tis_core.h"
 
+/* This is a polling delay to check for status and burstcount.
+ * As per ddwg input, expectation is that status check and burstcount
+ * check should return within few usecs.
+ */
+#define TPM_POLL_SLEEP	1  /* msec */
+
 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
 					bool check_cancel, bool *canceled)
 {
@@ -82,7 +88,7 @@ static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
 		}
 	} else {
 		do {
-			tpm_msleep(TPM_TIMEOUT);
+			tpm_msleep(TPM_POLL_SLEEP);
 			status = chip->ops->status(chip);
 			if ((status & mask) == mask)
 				return 0;
@@ -224,7 +230,7 @@ static int get_burstcount(struct tpm_chip *chip)
 		burstcnt = (value >> 8) & 0xFFFF;
 		if (burstcnt)
 			return burstcnt;
-		tpm_msleep(TPM_TIMEOUT);
+		tpm_msleep(TPM_POLL_SLEEP);
 	} while (time_before(jiffies, stop));
 	return -EBUSY;
 }
Signature · 6 lines
2.13.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
Reviewed-by on v4 3/4
#25 lore raw

Re: [PATCH v4 3/4] tpm: reduce tpm polling delay in tpm_tis_core

Quoted 9 lines · Nayna Jain

The existing wait_for_tpm_stat() polls for the chip status after 5msec sleep. As per TCG ddwg input, it is expected that tpm might return status in few usec. So, reducing the delay in polling to 1msec.
Similarly, get_burstcount() function sleeps for 5msec before retrying for next query to burstcount in a loop. If it takes lesser time for TPM to return, this 5msec delay is longer than necessary.

Quoted 4 lines

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~14sec to ~9sec.

/Jarkko

Quoted 42 lines
---
 drivers/char/tpm/tpm_tis_core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_tis_core.c +8 -2
index 993328ae988c..8b55fdaa67f2 100644
@@ -31,6 +31,12 @@
 #include "tpm.h"
 #include "tpm_tis_core.h"
 
+/* This is a polling delay to check for status and burstcount.
+ * As per ddwg input, expectation is that status check and burstcount
+ * check should return within few usecs.
+ */
+#define TPM_POLL_SLEEP	1  /* msec */
+
 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
 					bool check_cancel, bool *canceled)
 {
@@ -82,7 +88,7 @@ static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
 		}
 	} else {
 		do {
-			tpm_msleep(TPM_TIMEOUT);
+			tpm_msleep(TPM_POLL_SLEEP);
 			status = chip->ops->status(chip);
 			if ((status & mask) == mask)
 				return 0;
@@ -224,7 +230,7 @@ static int get_burstcount(struct tpm_chip *chip)
 		burstcnt = (value >> 8) & 0xFFFF;
 		if (burstcnt)
 			return burstcnt;
-		tpm_msleep(TPM_TIMEOUT);
+		tpm_msleep(TPM_POLL_SLEEP);
 	} while (time_before(jiffies, stop));
 	return -EBUSY;
 }
Signature · 1 line
2.13.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

Tested-by
#26 lore raw

Re: [PATCH v4 3/4] tpm: reduce tpm polling delay in tpm_tis_core

Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Nayna Jain

The existing wait_for_tpm_stat() polls for the chip status after 5msec sleep. As per TCG ddwg input, it is expected that tpm might return status in few usec. So, reducing the delay in polling to 1msec.
Similarly, get_burstcount() function sleeps for 5msec before retrying for next query to burstcount in a loop. If it takes lesser time for TPM to return, this 5msec delay is longer than necessary.

Quoted 6 lines
Quoted 4 lines

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~14sec to ~9sec.

/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

author
#27 lore raw

PATCH v4 4/4 tpm: use tpm_msleep() value as max delay

In reply to Nayna Jain

Currently, tpm_msleep() uses delay_msec as the minimum value in usleep_range. However, that is the maximum time we want to wait. The function is modified to use the delay_msec as the maximum value, not the minimum value.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~9sec to ~8sec.

Fixes 3b9af007869("tpm: replace msleep() with usleep_range() in TPM 1.2/

2.0 generic drivers")

---
 drivers/char/tpm/tpm.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm.h +2 -2
index 4fc83ac7abeb..644de70de2cc 100644
@@ -528,8 +528,8 @@ int tpm_pm_resume(struct device *dev);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
-	usleep_range(delay_msec * 1000,
-		     (delay_msec * 1000) + TPM_TIMEOUT_RANGE_US);
+	usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
+		     delay_msec * 1000);
 };
 
 struct tpm_chip *tpm_chip_find_get(int chip_num);
Signature · 6 lines
2.13.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
Tested-by on v4 4/4
#28 lore raw

Re: [PATCH v4 4/4] tpm: use tpm_msleep() value as max delay

Quoted 37 lines · Nayna Jain

Currently, tpm_msleep() uses delay_msec as the minimum value in usleep_range. However, that is the maximum time we want to wait. The function is modified to use the delay_msec as the maximum value, not the minimum value.

After this change, performance on a TPM 1.2 with an 8 byte burstcount for 1000 extends improved from ~9sec to ~8sec.

Fixes 3b9af007869("tpm: replace msleep() with usleep_range() in TPM 1.2/

2.0 generic drivers")

---
 drivers/char/tpm/tpm.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm.h +2 -2
index 4fc83ac7abeb..644de70de2cc 100644
@@ -528,8 +528,8 @@ int tpm_pm_resume(struct device *dev);
 
 static inline void tpm_msleep(unsigned int delay_msec)
 {
-	usleep_range(delay_msec * 1000,
-		     (delay_msec * 1000) + TPM_TIMEOUT_RANGE_US);
+	usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
+		     delay_msec * 1000);
 };
 
 struct tpm_chip *tpm_chip_find_get(int chip_num);
Signature · 1 line
2.13.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