Skip to content

Commit 9f3fc7b

Browse files
Hamza AttakJames Morris
authored andcommitted
tpm: replace msleep() with usleep_range() in TPM 1.2/2.0 generic drivers
The patch simply replaces all msleep function calls with usleep_range calls in the generic drivers. 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 <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]> Signed-off-by: James Morris <[email protected]>
1 parent 5d0e4d7 commit 9f3fc7b

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

drivers/char/tpm/tpm-interface.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
455455
goto out;
456456
}
457457

458-
msleep(TPM_TIMEOUT); /* CHECK */
458+
tpm_msleep(TPM_TIMEOUT);
459459
rmb();
460460
} while (time_before(jiffies, stop));
461461

@@ -970,7 +970,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
970970
dev_info(
971971
&chip->dev, HW_ERR
972972
"TPM command timed out during continue self test");
973-
msleep(delay_msec);
973+
tpm_msleep(delay_msec);
974974
continue;
975975
}
976976

@@ -985,7 +985,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
985985
}
986986
if (rc != TPM_WARN_DOING_SELFTEST)
987987
return rc;
988-
msleep(delay_msec);
988+
tpm_msleep(delay_msec);
989989
} while (--loops > 0);
990990

991991
return rc;
@@ -1085,7 +1085,7 @@ int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
10851085
}
10861086
} else {
10871087
do {
1088-
msleep(TPM_TIMEOUT);
1088+
tpm_msleep(TPM_TIMEOUT);
10891089
status = chip->ops->status(chip);
10901090
if ((status & mask) == mask)
10911091
return 0;
@@ -1150,7 +1150,7 @@ int tpm_pm_suspend(struct device *dev)
11501150
*/
11511151
if (rc != TPM_WARN_RETRY)
11521152
break;
1153-
msleep(TPM_TIMEOUT_RETRY);
1153+
tpm_msleep(TPM_TIMEOUT_RETRY);
11541154
}
11551155

11561156
if (rc)

drivers/char/tpm/tpm.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ enum tpm_const {
5050

5151
enum tpm_timeout {
5252
TPM_TIMEOUT = 5, /* msecs */
53-
TPM_TIMEOUT_RETRY = 100 /* msecs */
53+
TPM_TIMEOUT_RETRY = 100, /* msecs */
54+
TPM_TIMEOUT_RANGE_US = 300 /* usecs */
5455
};
5556

5657
/* TPM addresses */
@@ -527,6 +528,12 @@ int tpm_pm_resume(struct device *dev);
527528
int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
528529
wait_queue_head_t *queue, bool check_cancel);
529530

531+
static inline void tpm_msleep(unsigned int delay_msec)
532+
{
533+
usleep_range(delay_msec * 1000,
534+
(delay_msec * 1000) + TPM_TIMEOUT_RANGE_US);
535+
};
536+
530537
struct tpm_chip *tpm_chip_find_get(int chip_num);
531538
__must_check int tpm_try_get_ops(struct tpm_chip *chip);
532539
void tpm_put_ops(struct tpm_chip *chip);

drivers/char/tpm/tpm2-cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
899899
if (rc != TPM2_RC_TESTING)
900900
break;
901901

902-
msleep(delay_msec);
902+
tpm_msleep(delay_msec);
903903
}
904904

905905
return rc;

drivers/char/tpm/tpm_infineon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static int wait(struct tpm_chip *chip, int wait_for_bit)
191191
/* check the status-register if wait_for_bit is set */
192192
if (status & 1 << wait_for_bit)
193193
break;
194-
msleep(TPM_MSLEEP_TIME);
194+
tpm_msleep(TPM_MSLEEP_TIME);
195195
}
196196
if (i == TPM_MAX_TRIES) { /* timeout occurs */
197197
if (wait_for_bit == STAT_XFE)
@@ -226,7 +226,7 @@ static void tpm_wtx(struct tpm_chip *chip)
226226
wait_and_send(chip, TPM_CTRL_WTX);
227227
wait_and_send(chip, 0x00);
228228
wait_and_send(chip, 0x00);
229-
msleep(TPM_WTX_MSLEEP_TIME);
229+
tpm_msleep(TPM_WTX_MSLEEP_TIME);
230230
}
231231

232232
static void tpm_wtx_abort(struct tpm_chip *chip)
@@ -237,7 +237,7 @@ static void tpm_wtx_abort(struct tpm_chip *chip)
237237
wait_and_send(chip, 0x00);
238238
wait_and_send(chip, 0x00);
239239
number_of_wtx = 0;
240-
msleep(TPM_WTX_MSLEEP_TIME);
240+
tpm_msleep(TPM_WTX_MSLEEP_TIME);
241241
}
242242

243243
static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)

drivers/char/tpm/tpm_tis_core.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int wait_startup(struct tpm_chip *chip, int l)
5151

5252
if (access & TPM_ACCESS_VALID)
5353
return 0;
54-
msleep(TPM_TIMEOUT);
54+
tpm_msleep(TPM_TIMEOUT);
5555
} while (time_before(jiffies, stop));
5656
return -1;
5757
}
@@ -117,7 +117,7 @@ static int request_locality(struct tpm_chip *chip, int l)
117117
do {
118118
if (check_locality(chip, l))
119119
return l;
120-
msleep(TPM_TIMEOUT);
120+
tpm_msleep(TPM_TIMEOUT);
121121
} while (time_before(jiffies, stop));
122122
}
123123
return -1;
@@ -164,7 +164,7 @@ static int get_burstcount(struct tpm_chip *chip)
164164
burstcnt = (value >> 8) & 0xFFFF;
165165
if (burstcnt)
166166
return burstcnt;
167-
msleep(TPM_TIMEOUT);
167+
tpm_msleep(TPM_TIMEOUT);
168168
} while (time_before(jiffies, stop));
169169
return -EBUSY;
170170
}
@@ -396,7 +396,7 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
396396
priv->irq = irq;
397397
chip->flags |= TPM_CHIP_FLAG_IRQ;
398398
if (!priv->irq_tested)
399-
msleep(1);
399+
tpm_msleep(1);
400400
if (!priv->irq_tested)
401401
disable_interrupts(chip);
402402
priv->irq_tested = true;

0 commit comments

Comments
 (0)