diff mbox

msleep() delays - replace with usleep_range() in TPM 1.2/2.0 generic drivers

Message ID DF4PR84MB0028CFD4B5EBE9F929B83476F2A90@DF4PR84MB0028.NAMPRD84.PROD.OUTLOOK.COM (mailing list archive)
State New, archived
Headers show

Commit Message

Attak, Hamza July 10, 2017, 1:26 p.m. UTC
msleep() delays - replace with usleep_range() in TPM 1.2/2.0 generic drivers

Following the recent Nuvoton patch submitted by Mimi Zohar:
https://sourceforge.net/p/tpmdd/mailman/message/35685260/

Based on this work, I decided to apply a similar modification to the generic TPM drivers in the kernel.
The patch simply replaces all msleep function calls with usleep_range calls in the generic drivers. As a former patch already exists for the Nuvoton TPM, it is therefore not included in this new patch.

Tested with an Infineon TPM 1.2, using the generic tpm-tis module, for a thousand PCR extends, we see results going from 1m57s unpatched to 40s with the new patch. We obtain similar results when using the original and patched tpm_infineon driver, which is also part of the patch.
Similarly with a STM TPM 2.0, using the CRB driver, it takes about 20ms per extend unpatched and around 7ms with the new patch.

Note that the PCR consistency is untouched with this patch, each TPM has been tested with 10 million extends and the aggregated PCR value is continuously verified to be correct.

As an extension of this work, this could potentially and easily be applied to other vendor's drivers. Still, these changes are not included in the proposed patch as they are untested.

Signed-off-by: Hamza Attak <hamza@hpe.com>
---
 drivers/char/tpm/tpm-interface.c | 10 +++++-----
 drivers/char/tpm/tpm2-cmd.c      |  2 +-
 drivers/char/tpm/tpm_infineon.c  |  6 +++---
 drivers/char/tpm/tpm_tis_core.c  |  8 ++++----
 4 files changed, 13 insertions(+), 13 deletions(-)

Comments

Jarkko Sakkinen July 10, 2017, 7:15 p.m. UTC | #1
On Mon, Jul 10, 2017 at 01:26:51PM +0000, Attak, Hamza wrote:
> msleep() delays - replace with usleep_range() in TPM 1.2/2.0 generic drivers
> 
> Following the recent Nuvoton patch submitted by Mimi Zohar:
> https://sourceforge.net/p/tpmdd/mailman/message/35685260/

1. Please do not your work on a patch that was never applied.
   This one was https://patchwork.kernel.org/patch/9617835/ 
2. Please break your lines properly in the commit message
3. Please use commit ID in the mainline instead of URL.

Have you run scripts/checkpatch.pl?

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Attak, Hamza July 12, 2017, 12:33 p.m. UTC | #2
1. I guess you meant not to "base" my work on an unapplied patch. The patch we
   are submitting is not dependant on the referenced, earlier patch.
   It is also not including any of its changes. The former patch was only
   mentioned to give context, as it is a similar approach, however it is 
   addressing another problem (Nuvoton driver). This reference can as well be
   removed if misleading.
2. Linebreaks in commit message should be fixed now.
3. The URL has been removed, so as the reference to the former patch.

No, I didn't run checkpatch.pl when first submitting the patch. Testing it with
checkpatch.pl, the output gave me warning about lines over 80 chars. They are
now solved.
I am sending an updated version of the patch.

Thanks,
Hamza ATTAK.


-----Original Message-----
From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com] 
Sent: 10 July 2017 20:15
To: Attak, Hamza <hamza.attak@hpe.com>
Cc: tpmdd-devel@lists.sourceforge.net; Jacquin, Ludovic <ludo@hpe.com>; Edwards, Nigel <nigel.edwards@hpe.com>
Subject: Re: [tpmdd-devel] [PATCH] msleep() delays - replace with usleep_range() in TPM 1.2/2.0 generic drivers

On Mon, Jul 10, 2017 at 01:26:51PM +0000, Attak, Hamza wrote:
> msleep() delays - replace with usleep_range() in TPM 1.2/2.0 generic 
> drivers
> 
> Following the recent Nuvoton patch submitted by Mimi Zohar:
> https://sourceforge.net/p/tpmdd/mailman/message/35685260/

1. Please do not your work on a patch that was never applied.
   This one was https://patchwork.kernel.org/patch/9617835/
2. Please break your lines properly in the commit message 3. Please use commit ID in the mainline instead of URL.

Have you run scripts/checkpatch.pl?

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
diff mbox

Patch

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index bd2128e..f03daf5 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -395,7 +395,7 @@  ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 			goto out;
 		}
 
-		msleep(TPM_TIMEOUT);	/* CHECK */
+		usleep_range(TPM_TIMEOUT * 1000, (TPM_TIMEOUT * 1000) + 300);
 		rmb();
 	} while (time_before(jiffies, stop));
 
@@ -862,7 +862,7 @@  int tpm_do_selftest(struct tpm_chip *chip)
 			dev_info(
 			    &chip->dev, HW_ERR
 			    "TPM command timed out during continue self test");
-			msleep(delay_msec);
+			usleep_range(delay_msec * 1000, (delay_msec * 1000) + 300);
 			continue;
 		}
 
@@ -877,7 +877,7 @@  int tpm_do_selftest(struct tpm_chip *chip)
 		}
 		if (rc != TPM_WARN_DOING_SELFTEST)
 			return rc;
-		msleep(delay_msec);
+		usleep_range(delay_msec * 1000, (delay_msec * 1000) + 300);
 	} while (--loops > 0);
 
 	return rc;
@@ -977,7 +977,7 @@  again:
 		}
 	} else {
 		do {
-			msleep(TPM_TIMEOUT);
+			usleep_range(TPM_TIMEOUT * 1000, (TPM_TIMEOUT * 1000) + 300);
 			status = chip->ops->status(chip);
 			if ((status & mask) == mask)
 				return 0;
@@ -1045,7 +1045,7 @@  int tpm_pm_suspend(struct device *dev)
 		 */
 		if (rc != TPM_WARN_RETRY)
 			break;
-		msleep(TPM_TIMEOUT_RETRY);
+		usleep_range(TPM_TIMEOUT_RETRY * 1000, (TPM_TIMEOUT_RETRY * 1000) + 300);
 	}
 
 	if (rc)
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 881aea9..bdae205 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -961,7 +961,7 @@  static int tpm2_do_selftest(struct tpm_chip *chip)
 		if (rc != TPM2_RC_TESTING)
 			break;
 
-		msleep(delay_msec);
+		usleep_range(delay_msec * 1000, (delay_msec * 1000) + 300);
 	}
 
 	return rc;
diff --git a/drivers/char/tpm/tpm_infineon.c b/drivers/char/tpm/tpm_infineon.c
index e3cf9f3..86f0f3e 100644
--- a/drivers/char/tpm/tpm_infineon.c
+++ b/drivers/char/tpm/tpm_infineon.c
@@ -191,7 +191,7 @@  static int wait(struct tpm_chip *chip, int wait_for_bit)
 		/* check the status-register if wait_for_bit is set */
 		if (status & 1 << wait_for_bit)
 			break;
-		msleep(TPM_MSLEEP_TIME);
+		usleep_range(TPM_MSLEEP_TIME * 1000, (TPM_MSLEEP_TIME * 1000) + 300);
 	}
 	if (i == TPM_MAX_TRIES) {	/* timeout occurs */
 		if (wait_for_bit == STAT_XFE)
@@ -226,7 +226,7 @@  static void tpm_wtx(struct tpm_chip *chip)
 	wait_and_send(chip, TPM_CTRL_WTX);
 	wait_and_send(chip, 0x00);
 	wait_and_send(chip, 0x00);
-	msleep(TPM_WTX_MSLEEP_TIME);
+	usleep_range(TPM_WTX_MSLEEP_TIME * 1000, (TPM_WTX_MSLEEP_TIME * 1000) + 300);
 }
 
 static void tpm_wtx_abort(struct tpm_chip *chip)
@@ -237,7 +237,7 @@  static void tpm_wtx_abort(struct tpm_chip *chip)
 	wait_and_send(chip, 0x00);
 	wait_and_send(chip, 0x00);
 	number_of_wtx = 0;
-	msleep(TPM_WTX_MSLEEP_TIME);
+	usleep_range(TPM_WTX_MSLEEP_TIME * 1000, (TPM_WTX_MSLEEP_TIME * 1000) + 300);
 }
 
 static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
index c0f296b..e7d3046 100644
--- a/drivers/char/tpm/tpm_tis_core.c
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -51,7 +51,7 @@  static int wait_startup(struct tpm_chip *chip, int l)
 
 		if (access & TPM_ACCESS_VALID)
 			return 0;
-		msleep(TPM_TIMEOUT);
+		usleep_range(TPM_TIMEOUT * 1000, (TPM_TIMEOUT * 1000) + 300);
 	} while (time_before(jiffies, stop));
 	return -1;
 }
@@ -125,7 +125,7 @@  again:
 		do {
 			if (check_locality(chip, l) >= 0)
 				return l;
-			msleep(TPM_TIMEOUT);
+			usleep_range(TPM_TIMEOUT * 1000, (TPM_TIMEOUT * 1000) + 300);
 		} while (time_before(jiffies, stop));
 	}
 	return -1;
@@ -170,7 +170,7 @@  static int get_burstcount(struct tpm_chip *chip)
 		burstcnt = (value >> 8) & 0xFFFF;
 		if (burstcnt)
 			return burstcnt;
-		msleep(TPM_TIMEOUT);
+		usleep_range(TPM_TIMEOUT * 1000, (TPM_TIMEOUT * 1000) + 300);
 	} while (time_before(jiffies, stop));
 	return -EBUSY;
 }
@@ -408,7 +408,7 @@  static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
 	priv->irq = irq;
 	chip->flags |= TPM_CHIP_FLAG_IRQ;
 	if (!priv->irq_tested)
-		msleep(1);
+		usleep_range(1000, 1300);
 	if (!priv->irq_tested)
 		disable_interrupts(chip);
 	priv->irq_tested = true;