Re: [PATCH v7 07/10] tmp, tmp_tis: Implement usage counter for locality

From: Jason Andryuk
Date: Mon Jul 11 2022 - 15:39:32 EST


Hi,

This patch subject has a typo "tmp, tmp_tis" -> "tpm, tpm_tis"

On Wed, Jun 29, 2022 at 7:28 PM Lino Sanfilippo <LinoSanfilippo@xxxxxx> wrote:
>
> From: Lino Sanfilippo <l.sanfilippo@xxxxxxxxxx>
>
> Implement a usage counter for the (default) locality used by the TPM TIS
> driver:
> Request the locality from the TPM if it has not been claimed yet, otherwise
> only increment the counter. Also release the locality if the counter is 0
> otherwise only decrement the counter. Ensure thread-safety by protecting
> the counter with a mutex.
>
> This allows to request and release the locality from a thread and the
> interrupt handler at the same time without the danger to interfere with
> each other.
>
> By doing this refactor the names of the amended functions to use the proper
> prefix.
>
> Signed-off-by: Lino Sanfilippo <l.sanfilippo@xxxxxxxxxx>
> Tested-by: Michael Niewöhner <linux@xxxxxxxxxxxxxx>
> ---
> drivers/char/tpm/tpm_tis_core.c | 75 ++++++++++++++++++++++-----------
> drivers/char/tpm/tpm_tis_core.h | 2 +
> 2 files changed, 53 insertions(+), 24 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
> index bd4eeb0b2192..e50a2c78de9f 100644
> --- a/drivers/char/tpm/tpm_tis_core.c
> +++ b/drivers/char/tpm/tpm_tis_core.c

> @@ -215,6 +226,20 @@ static int request_locality(struct tpm_chip *chip, int l)
> return -1;
> }
>
> +static int tpm_tis_request_locality(struct tpm_chip *chip, int l)
> +{
> + struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
> + int ret = 0;
> +
> + mutex_lock(&priv->locality_count_mutex);
> + if (priv->locality_count == 0)
> + ret = tpm_tis_request_locality_locked(chip, l);
> + if (!ret)
> + priv->locality_count++;
> + mutex_unlock(&priv->locality_count_mutex);
> + return ret;
> +}
> +

This function should check that the requested locality matches the
current locality otherwise this sequence would seemingly succeed
though locality 0 is the one acquired.

tpm_tis_request_locality(chip, 0);
tpm_tis_request_locality(chip, 1);

Regards,
Jason