From patchwork Sun Nov 20 13:31:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13050039 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F6FCC43217 for ; Sun, 20 Nov 2022 13:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229587AbiKTNcq (ORCPT ); Sun, 20 Nov 2022 08:32:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbiKTNci (ORCPT ); Sun, 20 Nov 2022 08:32:38 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 148FDB34; Sun, 20 Nov 2022 05:32:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668951127; bh=mujM8XcZPtYkQ3B4FgSH2rYtTxc0/C3MRTBTnMu6dmk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=r6atztG5AplYF5bQD8RKXwicx5nJmkkIFzrgrPiuRAzsXzCV1UMMR5W9dZ52u8Hkr hIpr4O74UVkvPK7em3I8gD6yb8MVMSZqFZDggHLnT8NTnPSKAULdlIWDGy+v4M6BnF wS1kdeopMIsLS7B4SMinLcGw+zV+JdIDnKGtdwpjCjaacHaInm/kyjMOOq3XI+FWOL Al5hrkmXhVVGF8KZ/GO23d0eib56rTvnqdYEkGF5AOoAb6FQzl7akXpNCFIztEd490 55K1Qf0TDQSbYj7PvAU9VTpZDBfm8EnUfZ6OwS/6ByudeOVAJwFWxzs074jifXqdMq I4fQidtqfvD+w== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MXGrE-1oQLAv31VG-00Yf9l; Sun, 20 Nov 2022 14:32:07 +0100 From: Lino Sanfilippo To: peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca Cc: stefanb@linux.vnet.ibm.com, linux@mniewoehner.de, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, jandryuk@gmail.com, pmenzel@molgen.mpg.de, l.sanfilippo@kunbus.com, LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com Subject: [PATCH v10 09/14] tpm, tpm: Implement usage counter for locality Date: Sun, 20 Nov 2022 14:31:29 +0100 Message-Id: <20221120133134.28926-10-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221120133134.28926-1-LinoSanfilippo@gmx.de> References: <20221120133134.28926-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:a0lGO7ZgpoqQrWS0J6BFuHx+9CWESj/c6Q3CXkaKzSPe0+wurak gmT+Rn4Q/GpoXapCyXYECv0oyhy8RPfzTYmUJ49mz8wQvrcrEE+vVkwa2XARVEeahV8sq2x wq2IKgT2fhxdbJkfQ0jO4q0bLrGh0fWcskhMJb5XcOWq1fN3W5ePvF9wiFj7BGR5QbY3BgQ BiElQwsIQ4V2ijXngmCdg== UI-OutboundReport: notjunk:1;M01:P0:dtFF5JtrtR4=;xEMjq8XaQR1RpMrusqtbhQQ4LcL xhI7eEkOwhq5BO64PIxbW3krsIdXup0aOfx9EXcoPggXzwIjSsupBMAZHPMd+gqpFOAS39zSO RddG1c2/1CsNtqsSxguaZK03DgnX4+WFSxcNLT7e1zGuqoWM4tKrzwF8gBNO6xwSwvyApI3Gg fW51/Yf9VXZ6nFg7pglaiNKj5rbFbtJGr2UMpK9HdVq83hOFeEdCFrcznOoVRZDX054kqp8ZI S276xOU4PDlv/KMOIjz7xjXu2s1ym4ZpAx1Lm5M3T703BLIw4laofrm5PX8NVsk41ysWMm/nz QhgivpVTYL5TDP+NulT/Htsc9Ulryls2iXgrE1krSAAGPVObhEWiHnWF5CoC9btbg3bSHI7Vx yAtJzO7wyg+EOhC1xqXTOF9KOiohbnxj6cGW0Aor7KjVIhQypVPXPqlMd7esB1s0Ur28Jv9fU jaBqV6GVO9I+aahgNGYp5mr1EdG1TFb3n0CCfhjFgpAe3J6xhbI7qel0hdglJ1yv9w0mePcQw DYrm67rpBPX2GOysl9iFrQtED4HpRTHNksjCU0QPZmeChxI3sbw/ASjAXrKxmKm10rTGj8RgW HQH0S8Mnyd6FC0NRugS0TSj3ov8IK294CYqXT04YVIJBVNZMIEaiDudg4pFkej8e3nzUJRfiW rgMc6tn6Tv/ooJuSk6IKcSpGiW63gPvR8ngR4TRgYC6ouwd9wugw4vvl1PIZX3Kghp+Sx+RHc 1dvjzvN3U40Xlx8RATYYWIeoVKfot7Nr8+6lRiSkV2vTCKi+5PGplRAMzVDvq4SZuLci0wVKM 98ej9q8N90HxUQkJK4Ha3ObYqYhQoiEe5wdueyUuIYNV70BFrqwJHgDwKk7mCPQDHmEW2b/Pj olvuW2KtJCjSHqy/t0bF77AenUa6tpxbPZ1stePqthlPj3ogZ2b4OPjrlEUC+shmxNswinUqb 4w87sw== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo 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. Since in case of SPI the register accesses are locked by means of the SPI bus mutex use a sleepable lock (i.e. also a mutex) to ensure thread-safety of the counter which may be accessed by both a userspace thread and the interrupt handler. By doing this refactor the names of the amended functions to use a more appropriate prefix. Signed-off-by: Lino Sanfilippo Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 63 +++++++++++++++++++++++---------- drivers/char/tpm/tpm_tis_core.h | 2 ++ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index d0e5acd6b769..9aed9c26d2d5 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -165,16 +165,27 @@ static bool check_locality(struct tpm_chip *chip, int l) return false; } -static int release_locality(struct tpm_chip *chip, int l) +static int __tpm_tis_relinquish_locality(struct tpm_tis_data *priv, int l) +{ + tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); + + return 0; +} + +static int tpm_tis_relinquish_locality(struct tpm_chip *chip, int l) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); - tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); + mutex_lock(&priv->locality_count_mutex); + priv->locality_count--; + if (priv->locality_count == 0) + __tpm_tis_relinquish_locality(priv, l); + mutex_unlock(&priv->locality_count_mutex); return 0; } -static int request_locality(struct tpm_chip *chip, int l) +static int __tpm_tis_request_locality(struct tpm_chip *chip, int l) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); unsigned long stop, timeout; @@ -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(chip, l); + if (!ret) + priv->locality_count++; + mutex_unlock(&priv->locality_count_mutex); + return ret; +} + static u8 tpm_tis_status(struct tpm_chip *chip) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); @@ -682,7 +707,7 @@ static int probe_itpm(struct tpm_chip *chip) if (vendor != TPM_VID_INTEL) return 0; - if (request_locality(chip, 0) != 0) + if (tpm_tis_request_locality(chip, 0) != 0) return -EBUSY; rc = tpm_tis_send_data(chip, cmd_getticks, len); @@ -703,7 +728,7 @@ static int probe_itpm(struct tpm_chip *chip) out: tpm_tis_ready(chip); - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -787,14 +812,14 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, } priv->irq = irq; - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) return rc; rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), &original_int_vec); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -833,7 +858,7 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, rc = -1; } - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -949,8 +974,8 @@ static const struct tpm_class_ops tpm_tis = { .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, .req_canceled = tpm_tis_req_canceled, - .request_locality = request_locality, - .relinquish_locality = release_locality, + .request_locality = tpm_tis_request_locality, + .relinquish_locality = tpm_tis_relinquish_locality, .clk_enable = tpm_tis_clkrun_enable, }; @@ -984,6 +1009,8 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, priv->timeout_min = TPM_TIMEOUT_USECS_MIN; priv->timeout_max = TPM_TIMEOUT_USECS_MAX; priv->phy_ops = phy_ops; + priv->locality_count = 0; + mutex_init(&priv->locality_count_mutex); dev_set_drvdata(&chip->dev, priv); @@ -1062,14 +1089,14 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, intmask &= ~TPM_GLOBAL_INT_ENABLE; - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) { rc = -ENODEV; goto out_err; } tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); rc = tpm_chip_start(chip); if (rc) @@ -1103,13 +1130,13 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, * proper timeouts for the driver. */ - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) goto out_err; rc = tpm_get_timeouts(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); if (rc) { dev_err(dev, "Could not get TPM timeouts and durations\n"); @@ -1129,11 +1156,11 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, dev_err(&chip->dev, FW_BUG "TPM interrupt not working, polling instead\n"); - rc = request_locality(chip, 0); + rc = tpm_tis_request_locality(chip, 0); if (rc < 0) goto out_err; disable_interrupts(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); } } @@ -1200,13 +1227,13 @@ int tpm_tis_resume(struct device *dev) * an error code but for unknown reason it isn't handled. */ if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { - ret = request_locality(chip, 0); + ret = tpm_tis_request_locality(chip, 0); if (ret < 0) return ret; tpm1_do_selftest(chip); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); } return 0; diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 2deef11c88db..13bdcf38e56f 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -91,6 +91,8 @@ enum tpm_tis_flags { struct tpm_tis_data { u16 manufacturer_id; + struct mutex locality_count_mutex; + unsigned int locality_count; int locality; int irq; unsigned int int_mask;