Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 554b841d4703 ("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>

authored by

Mario Limonciello and committed by
Linus Torvalds
cacc6e22 22883973

+14 -5
+14 -5
drivers/char/tpm/tpm-chip.c
··· 521 521 return tpm_get_random(chip, data, max); 522 522 } 523 523 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 + 524 535 static int tpm_add_hwrng(struct tpm_chip *chip) 525 536 { 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)) 528 538 return 0; 529 539 530 540 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), ··· 639 629 return 0; 640 630 641 631 out_hwrng: 642 - if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip)) 632 + if (tpm_is_hwrng_enabled(chip)) 643 633 hwrng_unregister(&chip->hwrng); 644 634 out_ppi: 645 635 tpm_bios_log_teardown(chip); ··· 664 654 void tpm_chip_unregister(struct tpm_chip *chip) 665 655 { 666 656 tpm_del_legacy_sysfs(chip); 667 - if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip) && 668 - !(chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)) 657 + if (tpm_is_hwrng_enabled(chip)) 669 658 hwrng_unregister(&chip->hwrng); 670 659 tpm_bios_log_teardown(chip); 671 660 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))