Skip to content

Commit cacc6e2

Browse files
superm1torvalds
authored andcommitted
tpm: Add a helper for checking hwrng enabled
The same checks are repeated in three places to decide whether to use hwrng. Consolidate these into a helper. Also this fixes a case that one of them was missing a check in the cleanup path. Fixes: 554b841 ("tpm: Disable RNG for all AMD fTPMs") Signed-off-by: Mario Limonciello <mario.limonciello@amd.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2288397 commit cacc6e2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,20 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
521521
return tpm_get_random(chip, data, max);
522522
}
523523

524+
static bool tpm_is_hwrng_enabled(struct tpm_chip *chip)
525+
{
526+
if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
527+
return false;
528+
if (tpm_is_firmware_upgrade(chip))
529+
return false;
530+
if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
531+
return false;
532+
return true;
533+
}
534+
524535
static int tpm_add_hwrng(struct tpm_chip *chip)
525536
{
526-
if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM) || tpm_is_firmware_upgrade(chip) ||
527-
chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
537+
if (!tpm_is_hwrng_enabled(chip))
528538
return 0;
529539

530540
snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
@@ -629,7 +639,7 @@ int tpm_chip_register(struct tpm_chip *chip)
629639
return 0;
630640

631641
out_hwrng:
632-
if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
642+
if (tpm_is_hwrng_enabled(chip))
633643
hwrng_unregister(&chip->hwrng);
634644
out_ppi:
635645
tpm_bios_log_teardown(chip);
@@ -654,8 +664,7 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
654664
void tpm_chip_unregister(struct tpm_chip *chip)
655665
{
656666
tpm_del_legacy_sysfs(chip);
657-
if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip) &&
658-
!(chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED))
667+
if (tpm_is_hwrng_enabled(chip))
659668
hwrng_unregister(&chip->hwrng);
660669
tpm_bios_log_teardown(chip);
661670
if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))

0 commit comments

Comments
 (0)