From patchwork Fri Jun 10 11:08:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 12877479 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 EB750C433EF for ; Fri, 10 Jun 2022 11:09:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346627AbiFJLJc (ORCPT ); Fri, 10 Jun 2022 07:09:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348322AbiFJLJY (ORCPT ); Fri, 10 Jun 2022 07:09:24 -0400 Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E81EA14642B; 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=1654859344; bh=cRKDsGN0VdsCFTBnRcstCXzf84DjLh3s5S//H6evbxU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=RxvoEM2s+GCQWM8A+/5BrjdTPCe9c9CIHvswgUwXjKtrwa8Bh1/Cnfi68qad2XeMA /HDZYht5oWlChjbdATxTWqYm/u5vg3ff3iE6nmmsF0b4g6z4uaO4yGlNN2dL/RV4id NRLRDrLTC8i8mNBBt1TSn4DdoRFe4q3cgJQm5maA= 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 1MrhQ6-1nNEtv3Rky-00njsc; Fri, 10 Jun 2022 13:09:03 +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 01/10] tpm, tpm_tis: Avoid cache incoherency in test for interrupts Date: Fri, 10 Jun 2022 13:08:37 +0200 Message-Id: <20220610110846.8307-2-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:q8bU0YAnT48OWQ7N8IAEbPbVAzWl5CCrfbTQSDm5sbhGjdqIjlf LbPynvy15Ag5+DzsHakUiEJN8MAPK3p2HVIPpLQmW9Jzl9oSsSoxhzQYbvj+qFcNP9d5Xyo rMu8RFCICskwoiPKXu4yquCQ8+rtWeySkPHonWidwznKy+CcyivcOWXgVNob0aLl7v/Ms1S aJHRweNtbDjWhkWs5ujDA== X-UI-Out-Filterresults: notjunk:1;V03:K0:d09viExLtiY=:zlE2J+H0tdbp/D9Rgg8PAj UgB3M2NRHpR20pM03pyVo3iXvCpwIn6uPuMVqLE4jm7xzGex8nYgqZT35nvulLLlHPCVzcykg zqzYDdwiBthklFrHcM4cR2PfnzxiTlI+kXRiUyVDol+aAgXZpr1fwIonwjeK3f5vB5+JrUZvs 2Y5Z0XLEtOAIjWQ4+y9mhs6LwLRx2GVcpeafRwRhXtlO+iDpR2OM9aTZnrNMZyb6hFjkYopQ1 pv0dKmkquDNG2kcRoRpaG2T42T5wuL2ElX6GninO6sSUjNtfSwm+fC1KyzN1Fqzu1g38LBBHs JhxVjofqierOSIOve5aNfGw5E5L2M4nEmL4b0TRbkXqBSdB0trfEQqjkU9zTixCezgCoeFUQD BGYTlWm562KAlmqHgHtoYopbtQrMNmxzRbygsy3kBUbDHRxkJRh61LqhqyT4PiuGD/C/10sF5 CmXt3+3OZ/44E/mXiEO/jEAUauE6VmAJSoctpAF0lm2zxPESEmCrP6rAj6XdXf60Yh35tf0kY r+oNnVgE7PY99noQKCiaT1rQ/oTj05h2aZK8IHQxMvGOMR+eq9glEodkmrgQR3DwihfHoGwF2 xkMEXV0MDV3hVQ0r7AGVUJgrqzUXJ5INnM5OdyDJisDrpIU3DaCiyxZw1lYcbauRt4pEiRNwH f+RdFuzkO675eiVrKBFf1dCM2vdTbep6icKfWROLuz5pG4xIxx8t6jhaeFtELj9QuCx0e9YQx RUvFxunDP/SUP+NrIC9SdSxJxyR07l0nVV00MKMiuK567KhACO+SRfBe6luXJ1UUZw3yKBxQR m6kD9IqS4ZheaESBWcahczLpr0h6bgiWHK7EfzMUhLr6Fw//pD4UWm8k4bOYBizjGZqiLIF4n t/6gRjOD5HOReUHDnKsqOQAkYv6AUsS18XSkG95r8JdBGt21V/OuNwaFsVBsvjc0BO73roDF7 fA95DKkF8UWsCc8xLEU5qPxeis2huJioWO8kA/W7z0zq+Ulyf/pxqi6uOTuoQvpdBY8D8kGcq 3lejuEPHaBZjzHWYbW+gVjf3sn4VVcOHK78LpPrKt3q1woyYbcrCQmKz2qX2rsa8hAvBBthPM ZTmfIHcUrXou4du8VYN7iyb6S8YpwXnk/5Uff28GKsVuITRuQrBn8S+ZQ== 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 using a bitfield instead of a boolean variable and by accessing this field with the bit manipulating functions that provide cache coherency. Signed-off-by: Lino Sanfilippo --- drivers/char/tpm/tpm_tis_core.c | 13 +++++++------ drivers/char/tpm/tpm_tis_core.h | 6 +++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index dc56b976d816..6f2cf75add8b 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -470,7 +470,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->irqtest_flags)) return tpm_tis_send_main(chip, buf, len); /* Verify receipt of the expected IRQ */ @@ -480,11 +481,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->irqtest_flags)) tpm_msleep(1); - if (!priv->irq_tested) + if (!test_bit(TPM_TIS_IRQ_TESTED, &priv->irqtest_flags)) disable_interrupts(chip); - priv->irq_tested = true; + set_bit(TPM_TIS_IRQ_TESTED, &priv->irqtest_flags); return rc; } @@ -693,7 +694,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->irqtest_flags); if (interrupt & TPM_INTF_DATA_AVAIL_INT) wake_up_interruptible(&priv->read_queue); if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) @@ -779,7 +780,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->irqtest_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 3be24f221e32..0f29d0b68c3e 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -88,11 +88,15 @@ enum tpm_tis_flags { TPM_TIS_INVALID_STATUS = BIT(1), }; +enum tpm_tis_irqtest_flags { + TPM_TIS_IRQ_TESTED = BIT(0), +}; + struct tpm_tis_data { u16 manufacturer_id; int locality; int irq; - bool irq_tested; + unsigned long irqtest_flags; unsigned long flags; void __iomem *ilb_base_addr; u16 clkrun_enabled;