Unverified Commit e6fc76c8 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!13416 tpm: Lock TPM chip in tpm_pm_suspend() first

parents 0438e5a0 57a800b6
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -519,10 +519,6 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);

	/* Give back zero bytes, as TPM chip has not yet fully resumed: */
	if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
		return 0;

	return tpm_get_random(chip, data, max);
}

+21 −8
Original line number Diff line number Diff line
@@ -394,6 +394,13 @@ int tpm_pm_suspend(struct device *dev)
	if (!chip)
		return -ENODEV;

	rc = tpm_try_get_ops(chip);
	if (rc) {
		/* Can be safely set out of locks, as no action cannot race: */
		chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
		goto out;
	}

	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
		goto suspended;

@@ -401,19 +408,18 @@ int tpm_pm_suspend(struct device *dev)
	    !pm_suspend_via_firmware())
		goto suspended;

	rc = tpm_try_get_ops(chip);
	if (!rc) {
		if (chip->flags & TPM_CHIP_FLAG_TPM2)
	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
		tpm2_shutdown(chip, TPM2_SU_STATE);
		else
			rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);

		tpm_put_ops(chip);
		goto suspended;
	}

	rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);

suspended:
	chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
	tpm_put_ops(chip);

out:
	if (rc)
		dev_err(dev, "Ignoring error %d while suspending\n", rc);
	return 0;
@@ -462,11 +468,18 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
	if (!chip)
		return -ENODEV;

	/* Give back zero bytes, as TPM chip has not yet fully resumed: */
	if (chip->flags & TPM_CHIP_FLAG_SUSPENDED) {
		rc = 0;
		goto out;
	}

	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		rc = tpm2_get_random(chip, out, max);
	else
		rc = tpm1_get_random(chip, out, max);

out:
	tpm_put_ops(chip);
	return rc;
}