From patchwork Thu Nov 10 15:25:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038928 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 17002C4167E for ; Thu, 10 Nov 2022 15:26:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231519AbiKJP0j (ORCPT ); Thu, 10 Nov 2022 10:26:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231567AbiKJP00 (ORCPT ); Thu, 10 Nov 2022 10:26:26 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3F303F06B; Thu, 10 Nov 2022 07:26:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093943; bh=4XguBKph++G698gHdOWKE+P4P1KjkhqLb40WCE6oSOk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=rnUgDcOr9y3FYYOM12hPlWn6Cniki5dElw/ik4Zzeid7UpFtn0wq26zc5OQWlWHab u3i7ZcRA8rZma9XLG2acwJwpMM/kz0cbk3bOoakJMJ4G/H7rw4oTplRrKwuvE0WTwo pVP0bfQZllqPrrOXS0VGBJiM7MaCspgfvSKImRRN/2paoGWPH0eSLuIuw8S7uMfwaP VmE9ijnIuLtGLG4LWIimc7JcfAnUM4izEJSyedgn4Ta53akTQ52bSbo89lJhQx8ut0 9KLZomikbrk3s9BW9WNtRZqFXNtAlhykqJPGhgLPJdtOTXar/mjfzURQgfvDU/uCPz +ci4kk52jWmTg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MIdeX-1onO7u0FWv-00Eegq; Thu, 10 Nov 2022 16:25:43 +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 v9 01/12] tpm, tpm_tis: Avoid cache incoherency in test for interrupts Date: Thu, 10 Nov 2022 16:25:22 +0100 Message-Id: <20221110152533.24243-2-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:WxTfo7CHv952OlJ1ThoqcQEGG2O7RGYNejZ+VF9r78ydbiJt71A PRMLEYviU+2koO9qHtnzsIdlYyfvC0nBxm8+CijRxCH2RNMcAHZiJYuRQ0DoWiPsvIMjobZ NFr8iPyIF7Sm9gWCnhIAWFpN3YITBeqwxPir9xCrAfxmsS6EK4fnFFvRtlLio0XSmbEXsg2 grDENhve8F+b02nBlDtLA== UI-OutboundReport: notjunk:1;M01:P0:1Lq9YwGGrqs=;HkqIsNV81Qh823MNn9Y67CIzzVl Arp1LApSBsIjVhv/agtGm6HsFG1NHXLBPCivgIQ6VYQQmL7iQoZRXU5tj5u+oe6xfx1iIPMIQ hH8k10lYm1QeU8h4QqNaCJzCbFg7oK2tD09h/9+yndT3P7MJq1Mcgv0kGx97mxJWeovBhGtjO b7AO/YKmGuupE4YfjfYunayWr2IE73Bu457JIfMfGQsUCsxUw1t4a6ZHDLQAp2mqbj6n26sss Mp2HUmmCYzgvNNxqRl4T/g4wT2dakMfNHG+awdoAOLWN17lyhc+SAe5SYV318TZhoTrGx6CPr vwOLU1dCmIkcV1uAGDF9XyAYWx0KATwHcybylAJSchVPSfjJmtMxOPaYzXcewTW3TIybCrnvW oI3yPU4c7B0kXTe+jF/SVZrecdt5uf8txoTyjR7kL19Foq4qtTmYAjSP6nasYThA63icfjATJ VO4PwXMvcoZfIb65MeirV+ZM536sGMopIBVxj7hAaoMCFFSWZGJ39guMunfSpXvQh4nTmnDSq vsjflxUlhAVjuMPHhHBjKVNFjZ3a9YAyu1hMC/Nb+d5wRQW0oY7E/UoJ07epfYifXsj04+CwJ j0FcdLKBZwzdch6F6OrFftAjgR4z0/W8eFV2Qe0jjDRl33lekuB8PnDTN8HCYtl/Y/7cOFtjy qcwJlhMeD/JNo9YkzOSYumpQ9sy5e+0Cia4QnPcQeU7cJWsHPhDFE/4YPJGNfJATrZhhuN7RN OiFIBs4YcPxcpOh4HxGODcImfuVRCOfvaEKGkIjTrdmwLt+FiTD/hXs52vfKK95Rz1FQKe6TM QwDtfq2BjEhGXsedat93i/WIIJtX+ULk0FPR6qSCvytifbqaPq1kTS4vPfaskah0LTQECiFF+ USE2wipPDkQoCSFE+d3Y3reQ7NpXnR4m6lYEBjR7eaYabkZFTPYcuMYFuAaCfWNbA9//Jd3LE L4/rNA== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo The interrupt handler that sets the boolean variable irq_tested may run on another CPU as the thread that checks irq_tested as part of the irq test in tmp_tis_send(). Since nothing guarantees cache coherency between CPUs for unsynchronized accesses to boolean variables the testing thread might not perceive the value change done in the interrupt handler. Avoid this issue by setting the bit TPM_TIS_IRQ_TESTED in the flags field of the tpm_tis_data struct and by accessing this field with the bit manipulating functions that provide cache coherency. Also convert all other existing sites to use the proper macros when accessing this bitfield. Signed-off-by: Lino Sanfilippo Reviewed-by: Jarkko Sakkinen Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis.c | 2 +- drivers/char/tpm/tpm_tis_core.c | 21 +++++++++++---------- drivers/char/tpm/tpm_tis_core.h | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/drivers/char/tpm/tpm_tis.c b/drivers/char/tpm/tpm_tis.c index bcff6429e0b4..ce43412eb398 100644 --- a/drivers/char/tpm/tpm_tis.c +++ b/drivers/char/tpm/tpm_tis.c @@ -226,7 +226,7 @@ static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info) irq = tpm_info->irq; if (itpm || is_itpm(ACPI_COMPANION(dev))) - phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND; + set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags); return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg, ACPI_HANDLE(dev)); diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 757623bacfd5..c0008efb95dc 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -351,7 +351,7 @@ static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len) struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); int rc, status, burstcnt; size_t count = 0; - bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND; + bool itpm = test_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags); status = tpm_tis_status(chip); if ((status & TPM_STS_COMMAND_READY) == 0) { @@ -484,7 +484,8 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) int rc, irq; struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); - if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested) + if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || + test_bit(TPM_TIS_IRQ_TESTED, &priv->flags)) return tpm_tis_send_main(chip, buf, len); /* Verify receipt of the expected IRQ */ @@ -494,11 +495,11 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) rc = tpm_tis_send_main(chip, buf, len); priv->irq = irq; chip->flags |= TPM_CHIP_FLAG_IRQ; - if (!priv->irq_tested) + if (!test_bit(TPM_TIS_IRQ_TESTED, &priv->flags)) tpm_msleep(1); - if (!priv->irq_tested) + if (!test_bit(TPM_TIS_IRQ_TESTED, &priv->flags)) disable_interrupts(chip); - priv->irq_tested = true; + set_bit(TPM_TIS_IRQ_TESTED, &priv->flags); return rc; } @@ -641,7 +642,7 @@ static int probe_itpm(struct tpm_chip *chip) size_t len = sizeof(cmd_getticks); u16 vendor; - if (priv->flags & TPM_TIS_ITPM_WORKAROUND) + if (test_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags)) return 0; rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor); @@ -661,13 +662,13 @@ static int probe_itpm(struct tpm_chip *chip) tpm_tis_ready(chip); - priv->flags |= TPM_TIS_ITPM_WORKAROUND; + set_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags); rc = tpm_tis_send_data(chip, cmd_getticks, len); if (rc == 0) dev_info(&chip->dev, "Detected an iTPM.\n"); else { - priv->flags &= ~TPM_TIS_ITPM_WORKAROUND; + clear_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags); rc = -EFAULT; } @@ -707,7 +708,7 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id) if (interrupt == 0) return IRQ_NONE; - priv->irq_tested = true; + set_bit(TPM_TIS_IRQ_TESTED, &priv->flags); if (interrupt & TPM_INTF_DATA_AVAIL_INT) wake_up_interruptible(&priv->read_queue); if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) @@ -793,7 +794,7 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, if (rc < 0) return rc; - priv->irq_tested = false; + clear_bit(TPM_TIS_IRQ_TESTED, &priv->flags); /* Generate an interrupt by having the core call through to * tpm_tis_send diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 66a5a13cd1df..695a2516dce0 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -86,13 +86,13 @@ enum tis_defaults { enum tpm_tis_flags { TPM_TIS_ITPM_WORKAROUND = BIT(0), TPM_TIS_INVALID_STATUS = BIT(1), + TPM_TIS_IRQ_TESTED = BIT(2), }; struct tpm_tis_data { u16 manufacturer_id; int locality; int irq; - bool irq_tested; unsigned long flags; void __iomem *ilb_base_addr; u16 clkrun_enabled; From patchwork Thu Nov 10 15:25:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038929 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 27152C4321E for ; Thu, 10 Nov 2022 15:26:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229951AbiKJP0m (ORCPT ); Thu, 10 Nov 2022 10:26:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231625AbiKJP00 (ORCPT ); Thu, 10 Nov 2022 10:26:26 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6C213FB93; Thu, 10 Nov 2022 07:26:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093943; bh=1jiHlANZ8thD5aPn63BXGYT+VamFBFNIAjBcdG+2s2o=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=tBSnKYPgH6D47lLmnwgVr7yQSrBqh22q1ZdVRPBH9Qb2djQs6HBVqh3gn9UUbx22m +YAYJaMd8WZEuJrSHfXoRH3aPyFpxPg8uD1/77PoR7BoqtvKZvWBovykiyDkivK0Pq zTHWq1jI5R9aTeUXi5LYu12gMywSqr/hp6bcRF6tdHUpKPbPad36o1w5qSRzsJHHR5 JV3lWDUTd/WxPGfIRet6KM+COREg7QyvvLT1qdn2r/Zvmg55P9CsI4Ctv7agjUNBF8 7F2SdwJrjxqri1mTQ8Xo9W0P2RjYtY+2QZWQj3v/7zIRygt4MS2WUa3FNKPgY3nTh1 cikmRSeCdBFFA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Mwfac-1pDh7d1rP0-00y7Y7; Thu, 10 Nov 2022 16:25:43 +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 v9 02/12] tpm, tpm_tis: Claim locality before writing TPM_INT_ENABLE register Date: Thu, 10 Nov 2022 16:25:23 +0100 Message-Id: <20221110152533.24243-3-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:oYO90qW9He7dtb6gu2PAAleX0GX/mDToMlKMdoDqFAGkp+nPrxG 5WgmVLg2Pty/dWUtpqxRpsDGPHjWUhmY9pGoYGVcwdou9xk44L0dmxowttHxrMPOv5Hj2+H IFO7b0Z/MTh91TvfkYlNsshDZEhne8yiXVhO+DmvrK77h+sGRKv+1Z7IQOyyOGfal0yVGcE h3BpUy3vP49NWZTxAoAqg== UI-OutboundReport: notjunk:1;M01:P0:E75M3kG0FRw=;PoUhlxSyeF+FBtAAvBxELEZvhJD 22V3YfwXI1leI/YSBGvx7cAViigGaIHdBO/d8LBfZuGTZl5emWfQw62wt9EtrWMDvcPiFg3wY rSNR969pNvrTSHHf2Jm5WKaIMdTm0nH85yWr8D36E5JkpC7SN3GGCxJ+TCuzKoicvyZ/Tgq7Q EtvPw5YOGFjte3cLnwlq7oVSl+9QfS0WGE2BmdaPpRCjzQsmQQYvpeV9pVYCy0EGB6Yn7+yPe d5oUjdiITMDUB5/apF+dOhq9fYNjx5AOXeg299btiXsRvyG4TP9JvlFYrS4K3j8igT5GaH+7U t1R+en+BChgbqOevILAklwwVFE+M2VTFvA4O6kyU45Lrw6z4qWMGg7qC/N0DJngAgNuYOoVNu AGojVIBOfPh6Oq80Wk17fytuRBw8FCSTe45BPYsKpJz/PF1NuRa+MF2hLPqZefbyWAWq95QeK saOFaOEjWyRczYLzwwd65z5lznxsZrHRBKtUJvJqZrcnp2IuEku6PdKyCPVJXlLcxB6cDM+R/ qGeBO4AI+XbZOkZD1r5Jlk+K1aaXMxe4/wKNO2YpL+/utde2ZBcb4f3X6oih1LEGQ7f9qqkvu HkIDjwao+6OKBdhcF6ax6MqqJv8WR7GQisStBmVI6fTodZeNkUuswUUE4Qkhz+TI/oLVXc2dx 5izJsH5EDAexz7t0kVoIMzj97eCwD7njYUuc3fLC2hzoneo9C9iFGGv6ZIm3Tw9fsxWb9Uk+K VVB4unNwghLpnSMpDIob20cTxDCqF9iTWAdl6G2PGzYAt8ZkYSNWJwIrhG5/pqXMUKb0VvRJh skMGhLzgzgWR6TQmDs2PqHpQpELOkmjAeN7jy+hwgbLHMhDSkm/8mV6LlNWadKOHRTUvOQJG0 Noc2dHBHESUPqrtfWAQE0Jf1y+pJwNmJmzrmowJDploqmfPiIWizuEuYH3H70MpQ/8WyWExBo /LQvgA== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo In disable_interrupts() the TPM_GLOBAL_INT_ENABLE bit is unset in the TPM_INT_ENABLE register to shut the interrupts off. However modifying the register is only possible with a held locality. So claim the locality before disable_interrupts() is called. Signed-off-by: Lino Sanfilippo Reviewed-by: Jarkko Sakkinen Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index c0008efb95dc..d2c9c9d76893 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -1098,7 +1098,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); + if (rc < 0) + goto out_err; disable_interrupts(chip); + release_locality(chip, 0); } } else { tpm_tis_probe_irq(chip, intmask); From patchwork Thu Nov 10 15:25:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038935 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 19100C43217 for ; Thu, 10 Nov 2022 15:26:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbiKJP0q (ORCPT ); Thu, 10 Nov 2022 10:26:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231529AbiKJP03 (ORCPT ); Thu, 10 Nov 2022 10:26:29 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD77C40926; Thu, 10 Nov 2022 07:26:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093944; bh=vmr8i0HILdqGMwVhgOxfm//3j+C4M5iWtBnT+hrsrRw=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=l6NcozitBJOn/pRw0n3a9CgOrLtnM8YlA1d8jZtTFwPPvvU7Uw8RlNv36aQxrVrDc jO/lk2UZgewHknat5X1SMky/rs0cxy+zKwWj13rYjWEuPvPJkVsmWrdsfbWLL0MC1a 5G66ZhceVqdZ9Dguo/GaDgaVAPht+21cXuKqNujgyqvQlFGum240rpWCzxG7WsF86s tYvWiMzxeVNa0OAFOinONbYCPVRaRfZBVEp5UbMqp9CjevF7xQL4hrXuxWLHDX72jY rqVqLabBjcaw4U7NjQ5j9jWGG/IFxGFBlN3cU6uD5e8jQzHdZDFV7AcQoAlgfvi0HG +ffSnocqK481w== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M89Gt-1oxnzo3GrL-005L2E; Thu, 10 Nov 2022 16:25:43 +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 v9 03/12] tpm, tpm_tis: Disable interrupts if tpm_tis_probe_irq() failed Date: Thu, 10 Nov 2022 16:25:24 +0100 Message-Id: <20221110152533.24243-4-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:pM1M1Hc/4r3ew4oifeUS7mZ1Y9Ljsr4ABery3qVtBcuLO7/zlv7 gFYRo5fz8/wLYU0MK3psPh/O/bkkSbz+iZRBEF3VmbmChhSmtfnUxVIsKNUW+wV2up6Z7Ll ug2oXwHvSHth714q79SbJk+LpHWKSUemEddh5LyvLmQkhxdBD3u4vJe/XqZTa0Kkac/v4Q/ zwjk3Zz7241RR6XK7MReg== UI-OutboundReport: notjunk:1;M01:P0:AYfreQn/zm4=;T/7Y5nb/wGFzkRCd0y1uGLSzRRq NVQpG4fGGPcd6GI9Pz1n7S5NS7ZZ4Vj5MnA/0Dk7m4P+k2f0N0hyJl/DxSJgxaCCPsxP3kCMh LUlP3rZNA1CYjxNt5ULhwfTZ9E+rILTQRUhRVQk18TsBX8CG0HhfDzWXNs8ycea0aAU+bWCZ/ Vs/nic5NJiuzXBEAesZi9Q384TwU3wWCYlx4e88LHnNGp8175wJgj2WhgCVll41gF1m6sxdW4 QMSeBcTWQ2h/wlp11U6w4+/eK1Bf2Z/9IpqkFocUHExEjwnyGLByh/SPjAh/3oXYgHgEnPpzH pWZFIjKf2Lk7HuKzagion6EqlrtsShtcIwF/gY746f0gKjZrLpKUNsbCKAiteLuBETQE3Rz2f wrgnGxOuP1yOhcs5E59s//MZ/i0nsaRfjq2VNJfJar4rZlSYT7JRsOe5tUHLmDtsJXV7uW6YO Uy7Y5ofyplkXqm+bfxMTNyKhPyvHAhq3BUj96X0Q4L+VdV762lb5eDwJ6jrI4AiyC6ZlajFGr yfuNe1UnvQxV/Xo/KGpLBtV7uo4yUmMY/MY9G/2Zui9eWqx56Lkb55ZPJHdCQ30DdMZBNrmo3 LaBnluUAS4gG+nvdpNpEkT+u4jVvVpVSddhV8eV7E0ED/rQwoIIQD+lLesawyiTzfwvenvU2F yvnQvFreEID+6t4jn7W9c7UBQPPnloF5ndCZHwvk1a1rOVY2lpHVLiWScHJ+1nK/JFIrEM0Th 01OoI6T5iA0UB7E4JtTKbYddecVa/qKs9tOFXwXEqZa1xNak+AMJeeWx3vc58lHHHg9Sjxi6M WXOIoIP9ELwSDpMuRAWuShmDxO/YHJY1iMOtrwt1R3inMBG9C27rpsx5lNVDPrYsMxzwfT/Mx MVNz9DWOER33VqnkzOFKwYwjbJqsLL0tI+BpMPtS2VTFP2Evbv8OBK9tjaSIgprs+mDs9S/8r 3c64GQ== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo Both functions tpm_tis_probe_irq_single() and tpm_tis_probe_irq() may setup the interrupts and then return with an error. This case is indicated by a missing TPM_CHIP_FLAG_IRQ flag in chip->flags. Currently the interrupt setup is only undone if tpm_tis_probe_irq_single() fails. Undo the setup also if tpm_tis_probe_irq() fails. Signed-off-by: Lino Sanfilippo Reviewed-by: Jarkko Sakkinen Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index d2c9c9d76893..603b82ca56da 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -1091,21 +1091,21 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, goto out_err; } - if (irq) { + if (irq) tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED, irq); - if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { - dev_err(&chip->dev, FW_BUG + else + tpm_tis_probe_irq(chip, intmask); + + if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { + dev_err(&chip->dev, FW_BUG "TPM interrupt not working, polling instead\n"); - rc = request_locality(chip, 0); - if (rc < 0) - goto out_err; - disable_interrupts(chip); - release_locality(chip, 0); - } - } else { - tpm_tis_probe_irq(chip, intmask); + rc = request_locality(chip, 0); + if (rc < 0) + goto out_err; + disable_interrupts(chip); + release_locality(chip, 0); } } From patchwork Thu Nov 10 15:25:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038931 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 68EA5C07E9D for ; Thu, 10 Nov 2022 15:26:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230183AbiKJP0m (ORCPT ); Thu, 10 Nov 2022 10:26:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231571AbiKJP01 (ORCPT ); Thu, 10 Nov 2022 10:26:27 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D23613FB99; Thu, 10 Nov 2022 07:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093944; bh=A5gzN4mXM9p6SpYjvt1lyWJmsiJr8fpb+zizXUo+H9o=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=aQYQBr00g43DpNg8GIwA7D2/ec+yjFMyRLzCm+1dyHU3YM9oMFDvnMJbcyYmVaaTe NKd82ePb0x8TE5kqri7H9vT5gHl2dwIayrPSBORsh9rlVJZ+dOuMq+95p4NR8BOuli j2IT7XWLWVLRmDDeR+wk248ITd1SXnVIqV9fICua/UX797eQV8ff3nGs1WY3aMqMSk Zj7mySuI85pKWf6KQgccM7oGv4jGiMzDZsC291lfZ5Nzkow8vDvvm9/qRrd+rEuTXc J1bL1DbRZzmEpWxMChFjbst3RkiPRkHUBAoKnmwAjUM4IVXB61DSpqxCza+TMXSEax ZA3RjYvCViOOw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N1OXT-1p0Gqh0Zch-012mEg; Thu, 10 Nov 2022 16:25:44 +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 v9 04/12] tpm, tmp_tis: Claim locality before writing interrupt registers Date: Thu, 10 Nov 2022 16:25:25 +0100 Message-Id: <20221110152533.24243-5-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:9JTDDUP+gYrcmMFZPrNVpj07srKJyJojblE902lVdz7CnXEmkn+ nwSSGUKFfFNIGWWHdG/A+jG73iZcg2QVhPYDse+9pAYnS5dzKw3RJ0n5Daa9UhOy5qQNWiy dRkUa41KJkfekRHGHGxb2nJS8pyEfuclc6/s9FVfjQ80KMDyoBkMcQMhGDWtEVEJEkAEmDY 4bOJh2FDixzjOoNULIYtA== UI-OutboundReport: notjunk:1;M01:P0:99nGA77ETq8=;zqPhrmF94xabbY0BG0vFtOY/9zR EaSOZO7cx8OzDNHXr0yxk97BnfJ/qYdiTyrSZw49oNEAVtXtyKjnJAiSr7zw/3FX6Neq0ZVXt fcOnE+J1+xfAmZwRz8sD8BEsD+EKZr1QttLMqari3wEb2lDHFV3FThxBkvXNbNl0AArvbMsn2 e1B7XRgqgSF4GA+B/wM2f31zgVGiMUK+vhK/K8fYXy5mZOaEEEELeESxzCHIJIMXE0pGjxJ4T aTwC+4urnSbU0Ddoj5wpCSJccg2UOR/04yBkUPm3ViNiGR0scVQFzecmqUh7iKg4jgoNnlHj9 8ZypagiW2DxybRrsP4DLedUZEBY58A0jfBRxBOx5Wwgo6x7rORZmzj6POgD/JxpYHxdQV5pMi 8jClLjzZCIIA3HJaWvQKv8KSZWh7luuv60hyNa3LxVqRXbd5WHMdfgCqcrftTz+HIaBWyq9qN Pmi13NeT92C6b3Lm99IFfIetD3Efsv7fl4dHkpPleQQ8EnEbpQA77ri7jf3InnnQ1FMZRB1lf jqGhok366TmFHjX61vI+xkrYvClk5E8XCJ60Eur19udFw22QygiDCd0AJycNd0WjsYH5RML5c ZVRmApi2+ukLnb1WVB9LyjzC/e+nf5Epdhp/fKjX2bdscJyEr0yrQgZfqeP66Y5bH6bd90uL3 dTkQxj8qdxwlyJJhAUyCNIl2p9eR1XcKHZ0rW8vnlaa9bLui5KbRA2BaROBmOtWyjWdE6mruB Upkj6Nz9VHv9YPlfQhYJcR5UaOLGCyaLpKPPlQyr7RS/iCDbP5VCCc//3XC1uji5L6Tq6TO1S S0XU1VzqXWREUIOTuq0kl8DhKFbJDn5KF5GcpECHeXqXKJxba/xINY2CnAG873tRVC5cQe8xU hlfH2PfcdiL8HraYnruGxbiKBM6NBcluYt1OuClTvzzSa5CyF/SUxV3tYU4Mk9zHGM4+9XbgU eJDBSYKRQyEOzADh8RyPU08Z2Fc= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo In tpm_tis_probe_single_irq() interrupt registers TPM_INT_VECTOR, TPM_INT_STATUS and TPM_INT_ENABLE are modified to setup the interrupts. Currently these modifications are done without holding a locality thus they have no effect. Fix this by claiming the (default) locality before the registers are written. Signed-off-by: Lino Sanfilippo Reviewed-by: Jarkko Sakkinen Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 603b82ca56da..52205a1fee9e 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -770,30 +770,45 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, } priv->irq = irq; + rc = 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) + if (rc < 0) { + release_locality(chip, priv->locality); return rc; + } rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq); - if (rc < 0) + if (rc < 0) { + release_locality(chip, priv->locality); return rc; + } rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status); - if (rc < 0) + if (rc < 0) { + release_locality(chip, priv->locality); return rc; + } /* Clear all existing */ rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status); - if (rc < 0) + if (rc < 0) { + release_locality(chip, priv->locality); return rc; + } /* Turn on */ rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask | TPM_GLOBAL_INT_ENABLE); - if (rc < 0) + if (rc < 0) { + release_locality(chip, priv->locality); return rc; + } + release_locality(chip, priv->locality); clear_bit(TPM_TIS_IRQ_TESTED, &priv->flags); /* Generate an interrupt by having the core call through to From patchwork Thu Nov 10 15:25:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038924 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 361FDC4332F for ; Thu, 10 Nov 2022 15:26:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231483AbiKJP0h (ORCPT ); Thu, 10 Nov 2022 10:26:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231553AbiKJP0U (ORCPT ); Thu, 10 Nov 2022 10:26:20 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C382541996; Thu, 10 Nov 2022 07:26:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093944; bh=aGoHsAcDuhhqSpkLQAxXzsC5Nh/HzN5f5Q02AEnI5+I=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=S2cFlFG/Vry7WOQUPqyVgasb/4wtaHhK2q575cfcpu8TzoVE0FUXbDo6++eFPJwx0 xOJ9NRpHo8NTXzKZf7jDzHGZuj4219vjpRbja1CoacSGHQl00E4DYT325sFGVwNPBf 5hWbkpv3aRmWpS41Hdqpok65v1L2VyybPNlhBqBanry8yS/DVpCFD7BCvdpxHYoJuF esAhZFMBFbWIDbsLpYFXoTrYN8IvD4pyHFXdgr1THYiVC8s8cqH0sA9qK2aKVTZHGg siAKaSeUA9E1/ubCMUn0NMfGTHgfNRYR0ZNR0yqxHWXUbJmPKUgG9zZG0zxw2vrorn BRc6kFc21QC4Q== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Mkpap-1pGhGx1qrH-00mLoQ; Thu, 10 Nov 2022 16:25:44 +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 v9 05/12] tpm, tpm_tis: Only handle supported interrupts Date: Thu, 10 Nov 2022 16:25:26 +0100 Message-Id: <20221110152533.24243-6-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:U1FzkFHh+FaUFGJXtii0Y4f3oa8x6w1Azv0oAQk/rio/DK+5uZw vmpFKaWO/0yM4DSJkm2RspI9zgM2YzSsDAJc4LSYg/8nHF1uY2AEQbU4SEhz/UyxE2ZxCsF Rp1hLQiFN+Spvw5B6+qrsp16nQZGYoOQryGXTHKEAptzGJp6ChAcUPweKTs+PiCjvxcK8iN sE2WY1cV85ZP6oB1+s4yw== UI-OutboundReport: notjunk:1;M01:P0:+pT9Oktr0qw=;PZlJcadiBgNgxC8Ja21Zi8ysFHJ snDU4eBrgTvaze41E4ddqmMBD5oEr2i8SAllwGYQNQTgmZU25zH6eMdi20bo+9Y6ybkpRrWfx dOREYR2UafyXTfB8cN3eSbHa8GqkxRQJjDfo6zhJdWDVhL6DMDELQM3nNzIPd2YL5XHbIg9XD BMwKUHrz6GTMEZvpmU3WiwDZKnn0gxFZz/bcS8lQAGyPPqlBW3uL+njbSrYvODYNsV23p6FUs gdMo8Qa1NdJSJRzuCUJJqwGElhtSJLMy2lAyjTWiprrtLqQQ7OUXzdEXm6ixvQByPEhNyuIfw 3lUCG9bYq27/w8pYKn/NWg39ohw/WXaPxHEJAQZnbymaGUfh+U3lSM5N9P05GoGuV9QHploK9 FXwnRzXoNXLJwBhrfgYvOywO9F5Wr9IwGKpLVuCZAoWlvOoIJeAxnrHd03LKTaTAtdSSATGMw iVyvVzeF582SM+IhYqvBLUlKxjzrJ3MIhHl06edC1wsXtkvkMVWGMHzPHni+nfwhXSPYNXdsz tHGpEuJP0sI8eSpGj51oxXucvi3s+4E+DpTwPb2gAALrXkn0NUf4MSKyS348mFV+C2IQ6AVT7 aFQ2ABUcepThAgLDxeVacI38WdRP7ZV5mi+D6OobsDF3EC2pgiRkjnjztpJ2h8vTGfYOQwiGB HPfTnDO10W7bF927TEXLHh7+/UekYfMhg1vd8f6zHvpUhr2QXBAVrQhmbPMgl8KmagiC6/8Jp JAUyE4FwJbh8lqoILQdqaoUGkkkYikNnxHhTCeA6IHHzTnqqLHDWtiPWgguK5SVLr/pmNTlvc FrwFJ/sgjlPoLcFc6FE4L2tf85p8HYyahjsT4aiqhLJgzBv3c8mMDudOUEs3jOfJc5wn1AvnS Ov8t4rQzrXgQYlOcWGsOCKrtQ/T4gODxE2wQNpVQk176WRg78nnlt0bthFiN8NtPEyzxmrydR 1EswIA== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo According to the TPM Interface Specification (TIS) support for "stsValid" and "commandReady" interrupts is only optional. This has to be taken into account when handling the interrupts in functions like wait_for_tpm_stat(). To determine the supported interrupts use the capability query. Also adjust wait_for_tpm_stat() to only wait for interrupt reported status changes. After that process all the remaining status changes by polling the status register. Signed-off-by: Lino Sanfilippo Tested-by: Michael Niewöhner Reviewed-by: Jarkko Sakkinen --- drivers/char/tpm/tpm_tis_core.c | 120 +++++++++++++++++++------------- drivers/char/tpm/tpm_tis_core.h | 1 + 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 52205a1fee9e..d07debc3182c 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -53,41 +53,63 @@ static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, long rc; u8 status; bool canceled = false; + u8 sts_mask = 0; + int ret = 0; /* check current status */ status = chip->ops->status(chip); if ((status & mask) == mask) return 0; - stop = jiffies + timeout; + /* check what status changes can be handled by irqs */ + if (priv->int_mask & TPM_INTF_STS_VALID_INT) + sts_mask |= TPM_STS_VALID; - if (chip->flags & TPM_CHIP_FLAG_IRQ) { + if (priv->int_mask & TPM_INTF_DATA_AVAIL_INT) + sts_mask |= TPM_STS_DATA_AVAIL; + + if (priv->int_mask & TPM_INTF_CMD_READY_INT) + sts_mask |= TPM_STS_COMMAND_READY; + + sts_mask &= mask; + + stop = jiffies + timeout; + /* process status changes with irq support */ + if (sts_mask) { + ret = -ETIME; again: timeout = stop - jiffies; if ((long)timeout <= 0) return -ETIME; rc = wait_event_interruptible_timeout(*queue, - wait_for_tpm_stat_cond(chip, mask, check_cancel, + wait_for_tpm_stat_cond(chip, sts_mask, check_cancel, &canceled), timeout); if (rc > 0) { if (canceled) return -ECANCELED; - return 0; + ret = 0; } if (rc == -ERESTARTSYS && freezing(current)) { clear_thread_flag(TIF_SIGPENDING); goto again; } - } else { - do { - usleep_range(priv->timeout_min, - priv->timeout_max); - status = chip->ops->status(chip); - if ((status & mask) == mask) - return 0; - } while (time_before(jiffies, stop)); } + + if (ret) + return ret; + + mask &= ~sts_mask; + if (!mask) /* all done */ + return 0; + /* process status changes without irq support */ + do { + status = chip->ops->status(chip); + if ((status & mask) == mask) + return 0; + usleep_range(priv->timeout_min, + priv->timeout_max); + } while (time_before(jiffies, stop)); return -ETIME; } @@ -1021,8 +1043,40 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, if (rc < 0) goto out_err; - intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT | - TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT; + /* Figure out the capabilities */ + rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps); + if (rc < 0) + goto out_err; + + dev_dbg(dev, "TPM interface capabilities (0x%x):\n", + intfcaps); + if (intfcaps & TPM_INTF_BURST_COUNT_STATIC) + dev_dbg(dev, "\tBurst Count Static\n"); + if (intfcaps & TPM_INTF_CMD_READY_INT) { + intmask |= TPM_INTF_CMD_READY_INT; + dev_dbg(dev, "\tCommand Ready Int Support\n"); + } + if (intfcaps & TPM_INTF_INT_EDGE_FALLING) + dev_dbg(dev, "\tInterrupt Edge Falling\n"); + if (intfcaps & TPM_INTF_INT_EDGE_RISING) + dev_dbg(dev, "\tInterrupt Edge Rising\n"); + if (intfcaps & TPM_INTF_INT_LEVEL_LOW) + dev_dbg(dev, "\tInterrupt Level Low\n"); + if (intfcaps & TPM_INTF_INT_LEVEL_HIGH) + dev_dbg(dev, "\tInterrupt Level High\n"); + if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) { + intmask |= TPM_INTF_LOCALITY_CHANGE_INT; + dev_dbg(dev, "\tLocality Change Int Support\n"); + } + if (intfcaps & TPM_INTF_STS_VALID_INT) { + intmask |= TPM_INTF_STS_VALID_INT; + dev_dbg(dev, "\tSts Valid Int Support\n"); + } + if (intfcaps & TPM_INTF_DATA_AVAIL_INT) { + intmask |= TPM_INTF_DATA_AVAIL_INT; + dev_dbg(dev, "\tData Avail Int Support\n"); + } + intmask &= ~TPM_GLOBAL_INT_ENABLE; rc = request_locality(chip, 0); @@ -1056,32 +1110,6 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, goto out_err; } - /* Figure out the capabilities */ - rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps); - if (rc < 0) - goto out_err; - - dev_dbg(dev, "TPM interface capabilities (0x%x):\n", - intfcaps); - if (intfcaps & TPM_INTF_BURST_COUNT_STATIC) - dev_dbg(dev, "\tBurst Count Static\n"); - if (intfcaps & TPM_INTF_CMD_READY_INT) - dev_dbg(dev, "\tCommand Ready Int Support\n"); - if (intfcaps & TPM_INTF_INT_EDGE_FALLING) - dev_dbg(dev, "\tInterrupt Edge Falling\n"); - if (intfcaps & TPM_INTF_INT_EDGE_RISING) - dev_dbg(dev, "\tInterrupt Edge Rising\n"); - if (intfcaps & TPM_INTF_INT_LEVEL_LOW) - dev_dbg(dev, "\tInterrupt Level Low\n"); - if (intfcaps & TPM_INTF_INT_LEVEL_HIGH) - dev_dbg(dev, "\tInterrupt Level High\n"); - if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) - dev_dbg(dev, "\tLocality Change Int Support\n"); - if (intfcaps & TPM_INTF_STS_VALID_INT) - dev_dbg(dev, "\tSts Valid Int Support\n"); - if (intfcaps & TPM_INTF_DATA_AVAIL_INT) - dev_dbg(dev, "\tData Avail Int Support\n"); - /* INTERRUPT Setup */ init_waitqueue_head(&priv->read_queue); init_waitqueue_head(&priv->int_queue); @@ -1112,7 +1140,9 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, else tpm_tis_probe_irq(chip, intmask); - if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { + if (chip->flags & TPM_CHIP_FLAG_IRQ) { + priv->int_mask = intmask; + } else { dev_err(&chip->dev, FW_BUG "TPM interrupt not working, polling instead\n"); @@ -1159,13 +1189,7 @@ static void tpm_tis_reenable_interrupts(struct tpm_chip *chip) if (rc < 0) goto out; - rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); - if (rc < 0) - goto out; - - intmask |= TPM_INTF_CMD_READY_INT - | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT - | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE; + intmask = priv->int_mask | TPM_GLOBAL_INT_ENABLE; tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index 695a2516dce0..2deef11c88db 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -93,6 +93,7 @@ struct tpm_tis_data { u16 manufacturer_id; int locality; int irq; + unsigned int int_mask; unsigned long flags; void __iomem *ilb_base_addr; u16 clkrun_enabled; From patchwork Thu Nov 10 15:25:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038925 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 6AE13C4321E for ; Thu, 10 Nov 2022 15:26:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231518AbiKJP0j (ORCPT ); Thu, 10 Nov 2022 10:26:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231516AbiKJP0Z (ORCPT ); Thu, 10 Nov 2022 10:26:25 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E04063F057; Thu, 10 Nov 2022 07:26:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093944; bh=BzRmHyDjx6k9QxSMFomIGBOCh5qkOm5olPta9ZMqnS8=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=oY6fc+MYBmRFOvgb1YCUuqd6X+pZtADhu3inWas+5FfA/rSVClrbBWfvPFhTl6djQ v9ej/mziiOQMsOxkFUgWGlsCSXzxAVhDPhKyN/xh6DQx6ieT6UHaNfa5bEEh4RxqPZ 51gng7vWATJ1935pn85laTRnf6KebPt66RJimf0sDUAYhUSkJSPu5ygNpHPKfQvIQo 2tzggNsFvU2TjLy4cg7roPmIBj5kZCnC0LkiKGIW96fL+6ZGaJkPOPMoA6Tp4+II4k fekEygrmS32NeEQ63LVuuiffk2hKMO7o1zkJZ/yvo6wUrZnU0zzaPaXu+bVP/DxiYZ EPzKUdXv1OQpQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MXp9Y-1oTwV23BQS-00Y6S9; Thu, 10 Nov 2022 16:25:44 +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 v9 06/12] tpm, tpm_tis: Move interrupt mask checks into own function Date: Thu, 10 Nov 2022 16:25:27 +0100 Message-Id: <20221110152533.24243-7-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:cFLkuomk+v0SkP7u8SezZBP73KBGJZF+fzCImxN4pepHBu5mmiO WmkjjVnyKIMm8XUfP1CeWH7OAbdnoIbnWYanzNGiKAY+F+5PL/F41fRNg0sp9A8LZm136lk TtzqNt0X+LzYIX/bfXTtDWSsMgWA2dqaDIaMXq4pqBC7ZqVcWDnwMbDB+bNeoE+/2+E3TMV EfrSj4atodSK8n4KR2JQQ== UI-OutboundReport: notjunk:1;M01:P0:lP6ySNlNswI=;hZlaPPrCIuE9fbdQsSy5xcOLm7o Y1+ZA49ssqiuYcDb/0XdwAYWFCM/yHjRT7WOgAZz4xtX/36EriSQ4RH/btxVIpaCr9htSdkoN jhLfjOYF4i1pPetZSWJS0q7aRUT9D9xKXwBDKXtwsUIgePBl5GNo41756hkkmtitsgfz/a8nF omkPJvjMsYJpwIiG73JeziCK/0gGNZMgYsYtlJHITM6PE1iyHQK+yxrsSVnRvTBNffSN0t0IY WQeVmAuykY/ThW6ikbSw3kQYla3AWQyvIDEM4LsGfLJM0NbphDsJKcX52MM1oyLzeEQGlImap 9qMH7eZcX0eqGZa+uNc5ZsKa/yjNQlkDGYaOrYBw3Po0p2o+xWIcuCb57D6js0uyy3mbKD0RK i8lQUZwh8OhyUFUDB1dhpIvJsmtIB+dB+FXIftHyThgll0VGGQbe8F+DtaHmx0/ydK8C7Exmf A7+vIW4uW3U8vm0O8xESS1di+xUfYPcggfx6uY/6E2vBrw2Rj8a2UwLvr9eg661GfY/Q7YQYY c6SpyuqU+x35azXxgAbnrZQFsNIL6CsfsgFJlzs4O8lB4ECIBoahX1p5QZzpRCkG6vJkeoQ+Q /jS7JLe7s1hD6B1mmG7UOGF4hQ9gG1s2P6aSaAfvUSrfWJUpSs3moni+o8/iFneVkGt6+26yH uDDum/OKxZs8Y3s5AKkPf1J50bDcbJzlBjtD76qjWBzZ/ZMVyNfIGK9QiAt1vizPZci08/mHx U92021RTCnjcwUylWhSIdpeMZ96n/527q4Tg0Rvcl32INyOwk7cYxYu3OHZ54kcQbiRNhkfQX rB8oisujW1e2g8ajYu85rspxHXrCOSdHbUS6HMJzdEqqum9QKm6QxqdvfWmREgxuzqhLMs9Pu 9Z1d2u5OBaZtU2RqekAaso2rxQGi6m75eN3nTPFEF107etCkQlVbmJYpI71JacBuf/PCZ6MGY NkI2hU4tEm5o2mTBKoB3rg5X9gw= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo Clean up wait_for_tpm_stat() by moving multiple similar interrupt mask checks into an own function. Signed-off-by: Lino Sanfilippo Suggested-by: Jarkko Sakkinen Reviewed-by: Jarkko Sakkinen --- drivers/char/tpm/tpm_tis_core.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index d07debc3182c..181c291b0bb8 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -44,6 +44,20 @@ static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, return false; } +static u8 tpm_tis_filter_sts_mask(u8 int_mask, u8 sts_mask) +{ + if (!(int_mask & TPM_INTF_STS_VALID_INT)) + sts_mask &= ~TPM_STS_VALID; + + if (!(int_mask & TPM_INTF_DATA_AVAIL_INT)) + sts_mask &= ~TPM_STS_DATA_AVAIL; + + if (!(int_mask & TPM_INTF_CMD_READY_INT)) + sts_mask &= ~TPM_STS_COMMAND_READY; + + return sts_mask; +} + static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, wait_queue_head_t *queue, bool check_cancel) @@ -53,7 +67,7 @@ static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, long rc; u8 status; bool canceled = false; - u8 sts_mask = 0; + u8 sts_mask; int ret = 0; /* check current status */ @@ -61,17 +75,10 @@ static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, if ((status & mask) == mask) return 0; + sts_mask = mask & (TPM_STS_VALID | TPM_STS_DATA_AVAIL | + TPM_STS_COMMAND_READY); /* check what status changes can be handled by irqs */ - if (priv->int_mask & TPM_INTF_STS_VALID_INT) - sts_mask |= TPM_STS_VALID; - - if (priv->int_mask & TPM_INTF_DATA_AVAIL_INT) - sts_mask |= TPM_STS_DATA_AVAIL; - - if (priv->int_mask & TPM_INTF_CMD_READY_INT) - sts_mask |= TPM_STS_COMMAND_READY; - - sts_mask &= mask; + sts_mask = tpm_tis_filter_sts_mask(priv->int_mask, sts_mask); stop = jiffies + timeout; /* process status changes with irq support */ From patchwork Thu Nov 10 15:25:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038927 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 EEBA3C4332F for ; Thu, 10 Nov 2022 15:26:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229829AbiKJP0k (ORCPT ); Thu, 10 Nov 2022 10:26:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231569AbiKJP01 (ORCPT ); Thu, 10 Nov 2022 10:26:27 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFB513C6FF; Thu, 10 Nov 2022 07:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093945; bh=NZ0b2tOq2Jj1mWbGc2AGV1zg/VmCzKBMKS0JDk1OzUg=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=JraCJdsKpqTaJ1tCduuIEmE+jAYDJpzWpdV2jYswMKYAuEZ/y5xjAMMDFv9Jpt43E JEtjkNPZYzBB7ZdZsimenVC5Ar4VddpJGl5M45xvFFDhgrS5tJQVGojoJJZc3NxM50 QSbQalNEqu0fhhstUAqwqPQbmR+L6PJPJq8XhGHU0Btozc/wtR7vHFJoiYF/YfbweI T6Z3AMYN9R6k0tD3wvEy/Vz8qcX/9FS6BZ99vRoBPwws4NeYwr2ZzOcuDe3/bZFgR8 KoLuErV2A1UuK+RzW4TyTw1zouoR8DLVHKyYXBy0gyd5AV3ufL4BZeBNhy44RskGd2 QRffVo0oqmM/A== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MwQTF-1p9Zj20CdZ-00sNBA; Thu, 10 Nov 2022 16:25:45 +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 v9 07/12] tpm, tpm_tis: do not check for the active locality in interrupt handler Date: Thu, 10 Nov 2022 16:25:28 +0100 Message-Id: <20221110152533.24243-8-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:GxP0aibOPHfomqlU2L62HC78GYwnrsSmJt5Ub7ZOMwbOcAXKjiZ TmH7+V1qV/nkWal0Chqm7Ie45EULFTBztt0nz6MsGxyDfTmEJYv5fktsf6RET4B7IT3benQ /vnTRy7nbIafZMQrbmt27nnhrsr1ZPIeME8jRZtj52XunV4xRG2EDK5Yq75ez6jNrcgOUoN lbI0d8/n0/8QfFEAvE6dg== UI-OutboundReport: notjunk:1;M01:P0:pE9h/nbfKPo=;amtKpgOOFt18QFUb+vzjAWN2835 uIquYovDCbBjPUvi+DhHtFa4miXr+EkvA+4+GmktxmLK8CYEkbqgK4PTlzqTDWvcaLH0PrW2Y R7k/Ic16SBH1ldZA8W5K52+FfKc9TwXzeTU3htqPcekjX3SGMswS/0vsEXzb/1hedV5y68VcL CRCh3dS51SrmROZBG6UQ10GMx5FFDfxVLzIaCAnJqoYg0XZoyo5okMQrtQF7SGcwRXMEN2qFr 39WaWxhjHrI5Y/e41Rz0OofushHhXXIVY2VK0J42R+Lmbxj17mL8A9ZNqH0l1gFLBrBDvuWo+ y6K+AWEEYFh/FHvZG9qq3WDRAz+27whaqJ2xXzLCjZBZzTJJhu1d/A6MVddZaix7J+LBewaTZ HCnSOp88z4gl8LO846ws3993XKNXuJrq/9rkI0Q/W/IH8l/1vGVt5uX+Wz3Gl5VcVD+AdaeoX D31efIBIBiUFNqdEkKVDENVM+awbqPfbkwKN9UAey91bo+/3NcbKzyulGOErleHaOE/cWFaZf pzQQOrizAmB+h/EBHY9ox4r9CF+pZ5ZhgqmayPb1zi+nqJTwzXsP3KU+/Vpqpj/BW795aVv/a 3kqJvNiLWR1LxxArrfjOFe21PusJxyUxX6VaBMB0IpHby6i1Twvn2WzdHW/0LYC+44xyK9m3K a3h0okqcUV89d3zetbXB/JpI7+Xgeeg662/MbSuJLAzFe9IzgHPr7vorVqGg1e0Zny3wFnIZe fLecKn3to3Okx0v0N4qpp/SJfvLRMGV8wc+l8/aJnXwgM3KHZcvC6+H9e4Y/tew7P5c4bQmzM /vJrw01ZLN3Gr+N8tcSZ/HDVT1C5LyZmIvF1jvvMUN3xSlJ409K6ygKU9vGYVIZpq67yYT3is LR3cFWhFf+t2QR+q9tjnQAjwZzsrZKbjPCi3qxuDdpwNeh2cUv3XmwK9ZQHeQ6wPSN+tg8fLd itOuV3FeF/lcPF52bGJ6c5OV/H0= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo After driver initialization tpm_tis_data->locality may only be modified in case of a LOCALITY CHANGE interrupt. In this case the interrupt handler iterates over all localities only to assign the active one to tpm_tis_data->locality. However this information is never used any more, so the assignment is not needed. Furthermore without the assignment tpm_tis_data->locality cannot change any more at driver runtime, and thus no protection against concurrent modification is required when the variable is read at other places. So remove this iteration entirely. Signed-off-by: Lino Sanfilippo Acked-by: Jarkko Sakkinen --- drivers/char/tpm/tpm_tis_core.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 181c291b0bb8..4336f7ea8c2b 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -728,7 +728,7 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id) struct tpm_chip *chip = dev_id; struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); u32 interrupt; - int i, rc; + int rc; rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt); if (rc < 0) @@ -740,10 +740,7 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id) set_bit(TPM_TIS_IRQ_TESTED, &priv->flags); if (interrupt & TPM_INTF_DATA_AVAIL_INT) wake_up_interruptible(&priv->read_queue); - if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) - for (i = 0; i < 5; i++) - if (check_locality(chip, i)) - break; + if (interrupt & (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT | TPM_INTF_CMD_READY_INT)) From patchwork Thu Nov 10 15:25: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: 13038926 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 73319C43219 for ; Thu, 10 Nov 2022 15:26:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231547AbiKJP0i (ORCPT ); Thu, 10 Nov 2022 10:26:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbiKJP0U (ORCPT ); Thu, 10 Nov 2022 10:26:20 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65A9F3E080; Thu, 10 Nov 2022 07:26:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093945; bh=3KlLEQb7IWTC+S751hHd5VTkqMQyRRQZBkiQMbL6Rjc=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=gmyFgNjDriHqVPT4KLgTD+F/ig5VEqBJHAgVJx+Tq5aHtNYj9ri/wetSH3DiAXkiX goKHhPTpTiSO1ymAVJbdSVarM2lCEn21lXU48RJz7Tgg31F3XxeQAAoiAaQsVx/4l4 w6uoaf46KknggE2HDkPWuWKcpRCb4I1pExKlod+Nbmg8dvOKGu1Vyor9M2SC0yNqzO hhXl+rAUCU/+Cj0oERKEIG7ALloN56Uh2olLew/eUyaFdf//xaDKfs9ZQjNEeT6Uuk fkJvhgKE3XmPxTuYU0NbJFwP75RyzAKJpIsDLlEdsT3ZywPGjuS53axwvukxofk4Kv ORLhooYdY2fIA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MeU4y-1pRNj31YKi-00aZJR; Thu, 10 Nov 2022 16:25:45 +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 v9 08/12] tpm, tpm: Implement usage counter for locality Date: Thu, 10 Nov 2022 16:25:29 +0100 Message-Id: <20221110152533.24243-9-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:TVTk0VgrwhN591otFJ9HqLORi9Da94m3kLRXwxfV8hnXxT8NjwS hrptZSUJRDHt3ftVOCF5mnodhgHY///HjyuTruamk44wCEHaH5yAK4gSoOZqN4mCylcZzJz iuhNJjbm2bDeOiGfEZOZzlH+HBsjEXQE0eg4JQmxoyAkxf0WUtP/mfAsHz3b2UzZwse2nJp ayc9YAuuNpotKEJZTLzcQ== UI-OutboundReport: notjunk:1;M01:P0:6Xy56185HPY=;Nnn/ICEXtr9XICdfhGGsCmBWmAJ zrYU52YGvBjihIVMRkXNwTB5CsQdbnja8ywNxCrRIFeyUshAkPxnqO49RAjqx+44cozD3dVKz ka4tBbSXSaBeRQp3mFYbNzxwCA+vEhrA4MZ51JlzkJlDiUogHI9R8DYRrO5+Jx/An8UnC1NWB r3ncEGyJYDA6GyVss3aFXqrYpH/QdwCatamBL27b2Z9llKlolgVBi34rBcaikn5RIdoODpaQK 233t+nI8Ewb9S882oD7Sov8DP7TNyfGdlqVMVP+9h7u/JmD3MOnBscBctAC2qVPohgwOC8pRK /ItyeEfxOpbDgfOIdJY75rAxh/hTWen14Dlw7PPehCGKvKhrAwPh/SbbXQGWZsv3oxGarEN3C jfkzdzccf1HP5aZP3Z7ZBC3sr7l044JP36FLRqvWgm5241koArQNcHHlxyrXHwu42ilGg5dmG BJWbrz617t37TCj/jAoKClWHQ59RMCCk+drB0CAEFMO8DYwPK2kmU3ltdcBqa3TZDFS+h2JUT WcAnTyHEHhSulkJ34cIw+dsKnfW8syQNN3EJ4i/zrzOHkfn5HhqiEpbfdRTX/si/EQDFXlV3R dVvna6qfN/ZYLAVcRl105rbSLDAlRC3yMQqkb2OH3JXJyQavPj4R3iXzs0LYtiBb+nmicoirt grQLy0Ik+PxE2rumWTGoUsDHPIazswPaxp/cepTIdIFrIgN63HrIlVVEUKC1oQscwbM4CMAlH hcu3HM7XeIWksqRh+X4E+3KmE40JSQfyTddPJs/Vw/+MoflFaKCsMyaQNx890zSa+oHgOYUvj PcIKIHUUNAganRixL6iA2k99cC8GxiTZinvLhBgVeDdXILG5pXgXUA4hzOzqJUO0w8lKaH1cf JGNaH5vGE8OA66ovG9YNXISt4AOwKS6pmwkNwTtCaBE6K53AcYqa1OHK+c0ZGXa4tdf2J2VRw uBW0038w2T/PRBl9HFp3N+96h9c= 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 | 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 4336f7ea8c2b..ecde684a8f80 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; } @@ -762,7 +787,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip) cap_t cap; int ret; - ret = request_locality(chip, 0); + ret = tpm_tis_request_locality(chip, 0); if (ret < 0) return ret; @@ -771,7 +796,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip) else ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0); - release_locality(chip, 0); + tpm_tis_relinquish_locality(chip, 0); return ret; } @@ -796,33 +821,33 @@ 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; } rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } /* Clear all existing */ rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } @@ -830,11 +855,11 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask | TPM_GLOBAL_INT_ENABLE); if (rc < 0) { - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); return rc; } - release_locality(chip, priv->locality); + tpm_tis_relinquish_locality(chip, priv->locality); clear_bit(TPM_TIS_IRQ_TESTED, &priv->flags); /* Generate an interrupt by having the core call through to @@ -970,8 +995,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, }; @@ -1005,6 +1030,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); @@ -1083,14 +1110,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) @@ -1124,13 +1151,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"); @@ -1150,11 +1177,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); } } @@ -1221,13 +1248,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; From patchwork Thu Nov 10 15:25:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038933 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 9D711C43217 for ; Thu, 10 Nov 2022 15:26:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231296AbiKJP0n (ORCPT ); Thu, 10 Nov 2022 10:26:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231573AbiKJP01 (ORCPT ); Thu, 10 Nov 2022 10:26:27 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D46FF3FBAD; Thu, 10 Nov 2022 07:26:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093945; bh=nqgwkXJ1VF0LrftbG+WhB8X63vmoXLOswuQ4uFOGVgY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Du/fYTIY8QM5kUQOMWwW+yoe74wBiBLkcObzmHernPtNUBh/Hr8ek8qepXmhgJRrj /Mhd5eRqpUQfDPfujoikMy4kC74QMGa8NxV7ySqkf/BvB+LfGNBvkivrNagguyk9MC hZle4bxtYUF/umCDkWadvbbvS85NCB37OhHifA214OTBefCHstwKKOtJnqUuY9MRFU N5WBBG3G4XHhlsEsq5Y7tBW1L25IrGOCW2jZ7alHplvfQFYWpwCrGZ0p7WZCdwB6Rv 3EoE8Bj7+L69ohfqPfAUCcxHj3dIJE7++U2kYU+DlpopQzEINnc6cVgfetwbhCFfQl VzIy9IZS8P9VQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MiaYJ-1pW2Hu2wmV-00fmFO; Thu, 10 Nov 2022 16:25:45 +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 v9 09/12] tpm, tpm_tis: Request threaded interrupt handler Date: Thu, 10 Nov 2022 16:25:30 +0100 Message-Id: <20221110152533.24243-10-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:u4QcJuVH+P/3GCogrdX2jH//mNIoFzb3BBPVjX1/s/IgGyxxBXx N7KA1zeCXDJWHrlxcIC0NrbuLqo8bfMEjpuJVHsWXE3legHWnGrjrlljr+xsJ0+PWA3BPgk 1GgQUu9ZrHiX5g2GjnPVHTbJOqFpQXJUDQc1uIYtqmTLRZlJ63GpjRMtbU9phXsmBmeyyIb d5zrFyQccRiRaXAuoVMvA== UI-OutboundReport: notjunk:1;M01:P0:tlE5Q4GK4Ns=;RC6GCSG6I+BsDu/lmy398p1YnRy 2pinMMplLLNIGpfkKrAKVRZuCIwUCGVmYl9niSIewPYvGewCphyC2pH24TjYxyU41cNXEk3g7 brq0jvjxWK4MBLMWouJQvDOnIJzdyp13zeKfHBeebwUF+N2bdbmH0mCt7VhxNbIExiKgdqL6t 20ftIQKuHkqSG44aoaTi/JN5Zc4JtaBoIusle78Fs5U8HEj9+Cm86SV0z4fnXyBHbSKx+BGBM yBOT3tkvs+XL7NJ7HZIiv+fKB3fszQYkL/La7LGiSgXmhsnhSixvrV+LrCGEIlui9i5plHaxk T2bXBxQzKMASMZJYUCn4EwEmKFDkzIDsRnwuU9K1PMuKbn/x63ZnJ0kiS2MQwHVLUfPEyTX8S 3aYrp0h/jK5j4wbzsRdQhA+20BNgm3PWojHpGljV6PIs+/IJmyE0OseQnJq0/3VIzulu21z10 DSkJiQudVwvB1p0Rz54HtG9qaOIIKBv66uFUnNxR+s7BvDeEWxm6nPWlRQYH+8zy3/kjz8w5z CQ9nbqfxJqwG7nY4NnPtRhsTBhhTDOcxZV8UMaJZhgV2XQzTA4eU7eiAGmEmRbsBTEHbXKXJ7 KYNO1NLrnffD+UsoDccaOSXsVqkd8aZ9oTI9gKuVDAVjl5oUJgMsWUSBJjpqKBGPqz4Npf8yq w7vc/M+kJ8/HzBU9SSchxilyekR434GVoDAW9EUZJvBFY7SWLgqeyLWaNS9f8hr5GhWEEZM/U nmvak3g+HRWMvRlNx8sx1FDRmmKBEHQvyka3Si+iDLXn4ddN2PKDEWxVJ1n/Iv/Gis/SMxvxb +60Q5F7/PR9oX4pzQvVOwTQ1c7SUUlkmFmsHz72i552ozpPmn0/3mTUi4n3bKlQ33bCoZ/K+Y DHDnEbwNXRfCJzY1ofY6xh5sVoFNDxurfNrE1zK4k40frAwwPnmyejd7DRFbrnpdF/Uawnpyz isIqlCsN8vLOO9wUq18tcJNa794= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo The TIS interrupt handler at least has to read and write the interrupt status register. In case of SPI both operations result in a call to tpm_tis_spi_transfer() which uses the bus_lock_mutex of the spi device and thus must only be called from a sleepable context. To ensure this request a threaded interrupt handler. Signed-off-by: Lino Sanfilippo Tested-by: Michael Niewöhner Reviewed-by: Jarkko Sakkinen --- drivers/char/tpm/tpm_tis_core.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index ecde684a8f80..ced8eae3e4d5 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -813,8 +813,11 @@ static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, int rc; u32 int_status; - if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags, - dev_name(&chip->dev), chip) != 0) { + + rc = devm_request_threaded_irq(chip->dev.parent, irq, NULL, + tis_int_handler, IRQF_ONESHOT | flags, + dev_name(&chip->dev), chip); + if (rc) { dev_info(&chip->dev, "Unable to request irq: %d for probe\n", irq); return -1; From patchwork Thu Nov 10 15:25:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038930 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 6C061C41535 for ; Thu, 10 Nov 2022 15:26:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230397AbiKJP0l (ORCPT ); Thu, 10 Nov 2022 10:26:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231520AbiKJP01 (ORCPT ); Thu, 10 Nov 2022 10:26:27 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 920FC3FB97; Thu, 10 Nov 2022 07:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093946; bh=UIWWgQ8b2fsRQvPfPfHkm6StxCKq+xmUwy+9yaUc8mM=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=BjTOY1Xban+J7tuRw9hNZDmSQpSfFdGFoM74bNhMXFwvmdWwXD4e4NyQ++zOgAL4s hLKL7mWHv9BzDYs+xe6d9ILU80K+mQowxr9K/ojkEv+817WoBbxvajKkvFwL6WsUUI mw6FGOkOuPX2akB/wJxeXaSclPIE8vysxxvDaU8aZmtRhZIFfmzEt2ADjlLuN9YMkW bSE4+uqzw4PPi/pylCPhkfHygYMHhEX649WAch/v4EtqLxLP3iGVx+h4XTwqtdou/P X9Z0BkODbyMlbuWDDKLJy8Vcta3Q5WmN67u3mvJuQx/egRyxPIZU5y8w3tywnsfiyM DI2umVjQrcNdA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1McY8T-1pOukZ0B1Z-00d29g; Thu, 10 Nov 2022 16:25:46 +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 v9 10/12] tpm, tpm_tis: Claim locality in interrupt handler Date: Thu, 10 Nov 2022 16:25:31 +0100 Message-Id: <20221110152533.24243-11-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:2NYx2+Ps04Palgl1CFkfiIo2TjusG1YVxjgYjN2mHwYOy73ZM3U H8n/wi2pJbkHzUBOhGvW3nUkq2/M0wCFYzjta7Z0TMx4TvtUdArQhsph6w68hBRQnifEd7L bNS5bmuLQwGr1CRKQkrzO7XoVVgCZZ0f8M/Jbktx02kMK7xA5+Fbm84ScDHgpZ7uvst3vrU U0pb5hCGgledCXaQrg5tw== UI-OutboundReport: notjunk:1;M01:P0:RqxsIU8P8jU=;s67SjEbbNtsoRtz0NiA56GTdUZI dO0BdJIhcxeMJtooWqcsH/HxhzaBKrkA0YHiqEoA5kVPd1hu9r0WnsCUN/EBs+oJbHlaZ+8V6 WxvLQfc03RwIiCBM8rrqx9J8xX8BMLjCUemFHPBcN8LoPZbFpNLW9o1gJLXwmqgEXcsTBxOV6 Mm4xavlAqaNQgTVExNAk9Dwfvr2/8Bbbo9bMKnrZWrEGXfglg3UoUpoNl5sfOYz4HsL9NhOBL x1L4PFrKBE16JPONBEiHJl2/aBEb3eFaNuUIXBRKR46kx8M9X1ym041CJhWAlyA3baX/Zi0X4 trrdljAVoGYFYdMxOE6ieqQnautVv3xPtopg9PK2fi15rUelT83Mq0EnNA92E70X2+CtI4FIq 33hj+0vjw9EZDQWHK/JjWFx8e9+78+R48YAmZyQ10DNpRlE6tmMtmQjL5jU+EdBVn1Qj7QeFP mTrWvNJMxCNR2uw18+GnHEgGWg+Y+dEgZkoxLI+dlb9HQOgi+22GvWAMehRdOwCs/urS6iAXM EEzbe2MjIwtVOFKKqrLV2Yyw2UZoHLu2TcNSd1GAyrgK65slMpflhiKtKhkZnm4sgn4PgdSSq Ou9lv900KettuK6tH+kg0OVqJoODarszSJbv8glsl/Y8v/+DgUDWJcrL4WINjLEuknDRWZe7M vFqVdLe68ygRtwNp5vhbZiPSzL920geyX2eEsiX+kbDKfU8MWLHz4IY2YMzIy4IoFjMqWmXgT kvn2e/KrvdLEo4HrHpsbVN+WyQHH7XUwbEMHPgC2O1VaIxtvnSMCyILJj3PDm1IQqnfixnj2l JUoEBnVm3Mq/rIIEucnPGawbd4p4ABailymKtKU55plW+vRPfbxIbfysXhuh+ampBvgchsCO6 hssqwvQFvmLFc5yKY8jvaHElXaOZZORKA5BwTwJqRy1wik6GxIAZZkAKmYypkaszCjvjDMB5D 4vR1IFQV40K55Oav7HkKglytYs8= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo Writing the TPM_INT_STATUS register in the interrupt handler to clear the interrupts only has effect if a locality is held. Since this is not guaranteed at the time the interrupt is fired, claim the locality explicitly in the handler. Signed-off-by: Lino Sanfilippo Reviewed-by: Jarkko Sakkinen Tested-by: Michael Niewöhner --- drivers/char/tpm/tpm_tis_core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index ced8eae3e4d5..a0a1b1a3ddc6 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -772,7 +772,9 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id) wake_up_interruptible(&priv->int_queue); /* Clear interrupts handled with TPM_EOI */ + tpm_tis_request_locality(chip, 0); rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt); + tpm_tis_relinquish_locality(chip, 0); if (rc < 0) return IRQ_NONE; From patchwork Thu Nov 10 15:25:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038932 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 9D7E8C43219 for ; Thu, 10 Nov 2022 15:26:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230513AbiKJP0o (ORCPT ); Thu, 10 Nov 2022 10:26:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231149AbiKJP02 (ORCPT ); Thu, 10 Nov 2022 10:26:28 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD21140915; Thu, 10 Nov 2022 07:26:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093946; bh=btC8dh3Dw+tNF4g/eehZF3VWk3cjhFERu/V5ftrirJU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=KS4pwt5e7dF5v08WVnVrcx+livLGTde0Q8qRYi7YuKyWajffO3E0LyizhO/WuJuOJ 2oMCxeFmLe7Q+A58SOsYlEAQRpL/thR4//cU1/tus8oB7ZKE+8pfAHA47qCqljnXZ4 d9HOdXz6/6v/LyCsw1+TJf5H6A7mj8PL4tE4inpbUF1fenztDRtSzuxs/tN/4wRUKw yM9yXczC3A5ZllXUoB6exdBliS8WYFlUbU0ui3cIoia5pR525Y4poSLs8ipJZmUL2n NPrVfsquhc6VC2NGOt2+8frYuR6VfUm6GSGTGkj5t+LznYG7cBvzqjxChPWZzdk84+ E1WNpRyhvievw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Ml6qC-1pGQQI1UY0-00lWbB; Thu, 10 Nov 2022 16:25:46 +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 v9 11/12] tpm, tpm_tis: Claim locality when interrupts are reenabled on resume Date: Thu, 10 Nov 2022 16:25:32 +0100 Message-Id: <20221110152533.24243-12-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:A5G0ft3dSpBNzjqm1md9SHcjwaK4bKyVV/yoMeXJ+jVjZzq7VdY AuBcZPbCNhA4ddUre49DAerbGgjciksv0owtEqpPumDjCsPpptePlZ1fUTttnyiT5ifaKca 2Li0xgq0PZQ4JGvhumjQexAzOMxv3eWVjmzCz8MqRDeYU8tb7knd4TZccFrt5fkker0DUv5 7Za0l/odSyyJ/09Vgf5sw== UI-OutboundReport: notjunk:1;M01:P0:BBiN0L+fq3M=;NOc00iAhdsZJs8odzXGACJWmRyp uCnsIirG2oEPjoP+q0V9qNUUy7S4JdiQt7HrLzPsL0X2yT2YTW8qG3BfUMAkeiGJLWzj7mASU AuVmRfBohZ/d/fGuOiPewkiawHyhcyMWSbT0KmmJh1N//pjqeciY8D02KEETJcG7kbJJpTGue 8+gAMUQ8R2sMHMQDPN4FnwgzJIIrw/EmJ+KFN4JdM6ql6gJKYPJZwUpqhZ13PvmK8jc1zaSU6 diVlB0KxgEZsey1OYionJAZ3XjcCNzEp7BHY6D/HW0YZ5Dbl9ugWr1jfxioQ0HEELhNGpVqrC MLmFUc7yzC0//NeNlmIUWGi5PAqsHofPevpOP8rGlNzFs5ZOM6N2jeBMeghhL/meQD9gPXx7T vS73n+cLJQ76ccWxbJhOoKjujbofhmSGkW1H2dF62UxB0X9EVqK12S6po5j1jQLPdbBQw4Xnp wFvUJNdWn+zmBjM9kE5BfQ3c2AxhjN2FVgN6d1Pocjs/PHLykwrCYAEOApKyDbH7gYlZ8e19t Mws4BAdFMQCtMObygTNAIzik+huYcBAjRc8KWsDwCdPwDn/ENfALXyqR9u8zlCpU1hEhjT3o4 15v6uMwnEpDFqciHAuY74cuMzop2aif2FTviSK5pXGfkjRLtvukDY+Dy4SBFvMoTTBVuqaSH4 TUeEV5tC9B1J40k0OHF45oh3KKrpxZ56rAWWx8jnT97qRyKas1MPba3ANWqbdwLdh8+y8gnbT KTL6Cs85zNaUw4Z2nnKy1cPhOV9jPHNr6uQop7fX0oN5lajOVSK+XD1qbFF01sNKW3Cnq65T4 6PYF0SioAyxELoyfLOO6w42oojNq9tjJCXPx9cFsWEcAeotAzYOECP33Pe73YbR5OVPAHx0YF O/kHExO3eJwp0osupJ4WriN1/SNXWWE7PgxvQ3kMvmIaZPInYmz/K81ecxAIFws9fOp8fd5aG MrgduvusoZZxWhctZ3afVMlsZV0= Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo In tpm_tis_resume() make sure that the locality has been claimed when tpm_tis_reenable_interrupts() is called. Otherwise the writings to the register might not have any effect. Signed-off-by: Lino Sanfilippo --- drivers/char/tpm/tpm_tis_core.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index a0a1b1a3ddc6..469a1db95941 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -1241,28 +1241,27 @@ int tpm_tis_resume(struct device *dev) struct tpm_chip *chip = dev_get_drvdata(dev); int ret; + ret = tpm_tis_request_locality(chip, 0); + if (ret < 0) + return ret; + if (chip->flags & TPM_CHIP_FLAG_IRQ) tpm_tis_reenable_interrupts(chip); ret = tpm_pm_resume(dev); if (ret) - return ret; + goto out; /* * TPM 1.2 requires self-test on resume. This function actually returns * an error code but for unknown reason it isn't handled. */ - if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { - ret = tpm_tis_request_locality(chip, 0); - if (ret < 0) - return ret; - + if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) tpm1_do_selftest(chip); +out: + tpm_tis_relinquish_locality(chip, 0); - tpm_tis_relinquish_locality(chip, 0); - } - - return 0; + return ret; } EXPORT_SYMBOL_GPL(tpm_tis_resume); #endif From patchwork Thu Nov 10 15:25:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 13038934 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 4CAD2C4332F for ; Thu, 10 Nov 2022 15:26:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbiKJP0o (ORCPT ); Thu, 10 Nov 2022 10:26:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231440AbiKJP02 (ORCPT ); Thu, 10 Nov 2022 10:26:28 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD2E740916; Thu, 10 Nov 2022 07:26:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1668093946; bh=Zv582plVPWfjvWSFZ+HC7O7wVU9329RHBLjGsq+Wxlg=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=sv8sIbiLVMNHGfnpIUKme8pM78n9hmI7t4nqaze2Blp+2pK6r1w1O1gtum0CE5pqv vl4elkm7gVhRaH4z998o9azfWM23ibCCVW3NlV7PHmYWSRcih82j90i5rrzxwfd9/6 e7imOvRDOvgQ9fi+f/nQPvbeljQFdY6XGlRgUeoP++Hq82C+KNykMFKV03WhfemfIj koDainuGZP6q2vJW//jJ1oQikQjO7akMs6Xu1hMj/BLXokyCI+/HyTHZuROugnML8i UFVyOz70rMoF6pVequTuaOkbx58GrxpOEwFBAlsKSzrFPgw1lxavjcj+fGAosuVT5n 8HoZS+G80FAIg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MtwYu-1p7eys2wOa-00uI3d; Thu, 10 Nov 2022 16:25:46 +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 v9 12/12] tpm, tpm_tis: Enable interrupt test Date: Thu, 10 Nov 2022 16:25:33 +0100 Message-Id: <20221110152533.24243-13-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221110152533.24243-1-LinoSanfilippo@gmx.de> References: <20221110152533.24243-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:9CsuxgDeZtyikWqXYyxaadfmCCDJ8wut//BXW/q07Vkw5oXZUrv DoBeST34tttiIfQXmDwi4dnbjqwY6/PpJP6y0gBGopQM7g0+5uO3L+JpJbkI0VINTx7kMJ7 S3YkK4JqESPC6qBzhRKPj4TKNjzSimpRGXrLgRt/HgZB7TyGTPyl5nqpAWejLmE+tkqtxjN 29MuiVyLtwgX6M6mVSwlQ== UI-OutboundReport: notjunk:1;M01:P0:46bxHaXbu58=;SAhuf1Syhgc7z0wEm4xorJlzv0r eBVFIN2B64Qv6gX+tfA1lWBblLNNR4EwD74iPPV7oBnOLCrT/uPOwW3HuW2flRZJEFuGPWUhb RMAMStXzvR6G2cN4zWzgNTu4Ehtj8BOkf/j3MJp+g/T/wMdxzaxzx+m1qAWfLomC6YHCTOGg1 wgykjclmezSSjYJwljhEBnC5Uunm+1Y67B2/h9++ZraAxU1c5sOt4x6C455gGqlISJffxC/lJ Bg5wsYLPSLZGQ9WvQPGHzZAGosCx3TkLE27cawlH0O/Ak8qxk3a5hzYbr4E7dWiauetGX8ITZ 7OicIYv9eJ07F51BdExqXYiRygaxq8OTJTBkqsEaU0qj5nK+2tfUUkq0uxOcyhiXDvGpnpXyn tZM61JaBCl9SGksqeD3xCFnGCNfJeC/cKo6ciQlk6ZxMJafuYXlEIwAQjdA3E1l/zCrqjb1Gj KLNvwIQHlXikpb/+M9wWro9ME+YfezoCR63NGw3ZIPaI2YrGDLeHiB+jFIii9hriTTG8RmmJu mns1Q1FUe8kplfcF+ZLe03yMeP0YlBkon4TBx31f53TKYQ1eN4umnTUKRchV3yR+WDycR7EaX CDSrB558HYGXSqbumpvNkJDjcbiyCZNRvGpWwiaJ4V9NhSe2zcBsci2RrAkdrTuc+thnWTqPW 5YqhNR9ieVds9sWiVxjKQHbBO43ePvLbL+jCAFdjlUtpCfov84q5IF0UJyh/5DIYiMSRRpStB M4MUF/Eq/Su3ppOC2UWriIuH8erOBEJwJfH+Yj+V3oDIvn//8z3XvhnvY/38vbL0Bb/G3AJKJ G+IfgpoEYwjRAM6kl7NhniTxehFnf4KVk8Hb5YY3g7k2oii/GVls0SMwTj1RNRB1U/AqRcaek MuNZq3j7tB9SiOkpQQvlLiOvdQQOAouAjVPUm/88CXJcfxfc0wxXeOLGTS169HxjlZNAwuLG6 25WiSA== Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Lino Sanfilippo The test for interrupts in tpm_tis_send() is skipped if the flag TPM_CHIP_FLAG_IRQ is not set. Since the current code never sets the flag initially the test is never executed. Fix this by setting the flag in tpm_tis_gen_interrupt() right after interrupts have been enabled and before the test is executed. Signed-off-by: Lino Sanfilippo --- drivers/char/tpm/tpm_tis_core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index 469a1db95941..1f78582264eb 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -793,6 +793,8 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip) if (ret < 0) return ret; + chip->flags |= TPM_CHIP_FLAG_IRQ; + if (chip->flags & TPM_CHIP_FLAG_TPM2) ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc); else