From patchwork Thu Nov 24 13:55:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13055008 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 A5C2CC43217 for ; Thu, 24 Nov 2022 13:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229630AbiKXN4h (ORCPT ); Thu, 24 Nov 2022 08:56:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230071AbiKXN4d (ORCPT ); Thu, 24 Nov 2022 08:56:33 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 424A1ECCD3; Thu, 24 Nov 2022 05:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1669298173; bh=fv8SjyD4xDuS845+FrgNrBwr4CQVNrdzJ9YRUMiA+TY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=lcI1cZuAQ5wvaNyZE2ryqYmERayGugVT8v30Rz2tx8ePi7YqKY4DUqScBesUGVBVZ 42EhV05+qTtJK/OfukMidyvXmQf4gt35gtG8y4gJ2maVY25i5sSj+hD6JhZPxZ3e1w ZW2lThEle+prgpPVoBWy4Kt7WB33rTUgDJ5dSuG3gzXDCKHcPHX6PlXHqEUqhDuvXQ PltNzr4PvdPbMOen+7/sv44xrYBjwxs3WB/LH4lQQpt7zo5EKNVOCroVvuYq1KxoBj lJTfCK74SpfqlQo7i/ZyrQrzTw1q5LWN3l86E+BWZ/s/Xwld699aU/W6omESjQSk8W /SIFDqwpd4OAA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MuUnA-1pFJp10EAU-00rbly; Thu, 24 Nov 2022 14:56:13 +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 v11 09/14] tpm, tpm: Implement usage counter for locality Date: Thu, 24 Nov 2022 14:55:33 +0100 Message-Id: <20221124135538.31020-10-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221124135538.31020-1-LinoSanfilippo@gmx.de> References: <20221124135538.31020-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:EARtN2yQKkakweGe45TiFY3vqXI2qDGZjL4gyp78j5cHVteVZv5 HF3CDFi80lqOezCuQexU50FPdC7jHz82F492j3aBJmwLcdKMVEaafWdF1Ed7FaGDz3TUXCw i8ObbYI5qXlaOcqPEausj+fmQ+PSB05XO4NMzQo6qgg3LlMM86D7DdLoO3To04pMISoL2Oe p34/cDpN+TJrzJb7zWHQg== UI-OutboundReport: notjunk:1;M01:P0:+jLTMtAND+4=;hqIEXAWKxiuPKFVUiViFreV/Ml6 Tz4XPEUrjoJC3sNeBfK48XhgbgwTtd74/le+W/XQnSd+j+Tm4iV5EO3C+v8+w2yQ5vrEhjcBx bDAJYuWbq9YJbLHcPgZ+YbX4pwsk0GtMYLfq6HoQjx7xwBNLr4CxVjO8dsZLjTgkgZQFoKDzO 8Mw2vmUzZ4E6D6LuuJm2vVs963Ph5WLQbzw7iv3nI7JxG6FLSogbSXGJIidIm57QNHZW+fsdC aLPlByevOrgjAkcTjRx2EHsczzKytJhbCGI4IlKe67rV1ysEletFo+T6eAAb8DW1JKDDSN+pE Yure738l2W5OpqNewln7/fXluSXjHQQgAKZwfuCSxKBTKbenz0zDa7QgGAXGouA3vo/pqo+yW qp8haKRjUCCpvjv8Iu51erz6xViPcxzkYPAdARgqiQ4fgX/uccXoAQK2NzqwcvwnWTK9YhIkx 632phS2rNVt19qPoKD0hKpJR5QnTbsBAYn6Nbl/WT2qtS5KGjtcZOhnGpbOdyt3pF+D7L2lc6 Arf7qJe606693CrRO1gumFTHr8PKf1Qv0zRmOSQg0lWUBuLYXug2ytMw1HrjZTZoEAFS0ato9 /318EyZfm8gSRg5JXmM1zH2aM1I/+zLX4/AUxL1IktDm7CxtWH+UyBXbNtgtDPT8iA1HmoOzy qWYnrljVRkIPhayxuWYfuXOr6vnZWGeu+u1ofwj1BTKBVugKfvLdaRUjIcc9XSmNVnNoj0SOD hSaWvR9fTe2n7WYlDziiAqh5oAeWtwGKN6ZmuaueOP2GVy0jXwjIRNTsAsuVJFxC6pIiBEVRI i8UaKU465yYWTwFjdvtpWIFQzWsL/+XBnOr5WdaypUMfyWsPYlnJCuRchOCMjTnkk0P66PEmc XfAS8vwvwRkSHyOhpVSunvj/r6VUb9egniD3tJxEi7YaWg89vBkRshT+vXo8wmNc29Uylxf4v 5Tn7e4RsIF6OykJPDegG7QV6Zak= 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 Reviewed-by: Jarkko Sakkinen --- 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 d705dfb64cf9..f00f1057fd27 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; } @@ -788,14 +813,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; } @@ -834,7 +859,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; } @@ -950,8 +975,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, }; @@ -985,6 +1010,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); @@ -1063,14 +1090,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) @@ -1104,13 +1131,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"); @@ -1130,11 +1157,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); } } @@ -1201,13 +1228,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;