From patchwork Fri Jun 10 11:08:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 12877476 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 21C56CCA47F for ; Fri, 10 Jun 2022 11:09:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345515AbiFJLJ3 (ORCPT ); Fri, 10 Jun 2022 07:09:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348108AbiFJLJX (ORCPT ); Fri, 10 Jun 2022 07:09:23 -0400 Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7FFC14642A; Fri, 10 Jun 2022 04:09:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1654859346; bh=O45VmH8NuxzGZD4XqpbBWPkt09hOK0EdvFcOYQOngco=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=XUJ4KEBOFPQEsECL8OmhSz9RoumufW+odPQuCknWFgKaS+1cwtdt2G0bUwELwSP4+ +GHGuMZmiQh0ekfiCtpfp2sNmXsnu44GtxyvDeDwjt98dc9DhynGODLQa2AkbDOsOX h8dWOlynCn2VasbYdKoEYy7Ah6J/yJfPFUZNOePQ= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from Venus.fritz.box ([46.223.3.165]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MxDkw-1noqxF3HVg-00xY1S; Fri, 10 Jun 2022 13:09:05 +0200 From: LinoSanfilippo@gmx.de 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, l.sanfilippo@kunbus.com, LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com Subject: [PATCH v5 07/10] tmp, tmp_tis: Implement usage counter for locality Date: Fri, 10 Jun 2022 13:08:43 +0200 Message-Id: <20220610110846.8307-8-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220610110846.8307-1-LinoSanfilippo@gmx.de> References: <20220610110846.8307-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:73KXtmhSPA6z0Ps5tcxg1HoBOwfkc5Jk0+wV0CmquaXV+7z05O3 aqDSmUVnsO3xpKCNG5BMtrLphY4YGBfJSpLR84thdCOTio3z1BPd1amY3YVcdo0rj1pzUZe Cl3STbGbuFRuF6/e5dIQlMrVk8iVGPslpyXPxh8fcigMqRgKs25+E2bED+PEc1nXaPUfseU eobIvmN/CKirKOqvu/4Zg== X-UI-Out-Filterresults: notjunk:1;V03:K0:reKjEVESRyE=:QMiPPmAMowHXw9o0jUspbw CajIooEWEI/df6zM8PGtE2wWmxIGfNxolS0a91dRjXDEsqY3HcoUgNNU3l1f+RQ47Dsn2eKtc t8WdFZqu1PemGArS3aIYS9aOKDUWd6WhlNliabGZtf2GJmWKdbqG6yiRTgJQ9wKqUnI5EqwBH Xatzkl/vqK66bYKGEq29VnE81ha6NZqJ2B0/KQpZ4ujZgZqBj4EAsOems9bjpKllkyzhOqBUK edVnEuA9KR9VXoSS5Va9OSmxBfiJ/Poo2JAZ6kMgY+xsJFIBIMraoh8tra5jafG7JwoviY9vf GQ7AAQtgrfegfIuBtyBDAQa0Jj0vtwJDKRh8yY8HT8snvzylx7oXfUNa4k5J1CffQlAyKxrFa tYMvkvNpoCuNXNrCVk5gw7E9Yy00hQNzFEHxCBtntLSc+ip3vWJlRTTyfh19f0tJKuzMxwa+W GOdTxXpW7g701IcTCas1W+Yo+m8FroNxOXeTMpZ3V1GsJHz1Q0mL+RGhRCnJmV89HGs3jzLO7 VVu4PFFM9SDxfxCmpDM1TiaeL3DvpOUnDEGSu1UC7/X1U05kYxdztvWPHGS1Xfbqu4jAAiF44 Em7W/LtM5s22lGGZkdJfIJGmlY+fZXGwkLv5JsZxpwoTgRWQdX7XHxpyB6xeHk/GiRyac+cel 6yFSmJi1WwWDxKnpfPrifcxLtWytPohLz3M5gs77dT3w4oL8lL4PXjFdowj78hRZb0P1125ok ktyBxcbdm1tnUxDhDKAKd73aegtz8LGSLwxYtfFgftT3DWngSj6HaggAlTsLE3spMDM34/qSG Xp2tWNwfCR2sHxu3OCoov8mBjj/HB/nRkRa4L9TFa75zZYW04CeUPMoDipXUdNLfXAQ1b+t5d 1APrec5jmZ1P9CnSlUEfOvGaASJm1PxzpWQNnl00P0Z2Ovm8gIXFLZnbxOv0HM29woI5MYaxr 05IJULpe2WXLa7kcm4UBfy2D/Rpk6Vqla4orjTdw9nrBShNG5xihQ+iJ49IjFkwhDW9TXRo60 3dmsmDKfwPvXoeG4j8BUbnHH9z0nbDoWjm0b62WOoWu2m52GIiDbqoerAm+tUrXYnBujJpI2R UalgGtv0TRNYpyGHRYwU++tElfmz2yfOHBeDcClOQNdKzAycFFeCJnYeQ== 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. 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. Signed-off-by: Lino Sanfilippo --- drivers/char/tpm/tpm_tis_core.c | 30 ++++++++++++++++++++++++++++-- drivers/char/tpm/tpm_tis_core.h | 2 ++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 028bec44362d..0ef74979bc2c 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -158,16 +158,26 @@ static bool check_locality(struct tpm_chip *chip, int l) return false; } +static int release_locality_locked(struct tpm_tis_data *priv, int l) +{ + tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); + return 0; +} + static int release_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) + release_locality_locked(priv, l); + mutex_unlock(&priv->locality_count_mutex); return 0; } -static int request_locality(struct tpm_chip *chip, int l) +static int request_locality_locked(struct tpm_chip *chip, int l) { struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); unsigned long stop, timeout; @@ -208,6 +218,20 @@ static int request_locality(struct tpm_chip *chip, int l) return -1; } +static int 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 = request_locality_locked(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); @@ -987,6 +1011,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); diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 8e02faa4079d..e1871c482da2 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -94,6 +94,8 @@ enum tpm_tis_irqtest_flags { struct tpm_tis_data { u16 manufacturer_id; + struct mutex locality_count_mutex; + unsigned int locality_count; int locality; int irq; unsigned int irqs_in_use;