From patchwork Mon Oct 17 23:57: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: 3848 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1696265wrs; Mon, 17 Oct 2022 16:59:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6dCBm2N+0kXEiM62JdgMakfo/y1GomyOO+s0zk+tSUbo/93V8FVbHfQEwV7PZM2E1ALau/ X-Received: by 2002:a17:907:86a2:b0:791:910e:cce4 with SMTP id qa34-20020a17090786a200b00791910ecce4mr200028ejc.36.1666051194716; Mon, 17 Oct 2022 16:59:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666051194; cv=none; d=google.com; s=arc-20160816; b=GEgzHao5ZVSA1MSSj7/lPKnZKLRsh1pJ9M/bPoqNlBSyB8pexEK3xm1qqEglz11kaE jrj8fUBT96EkXtdQMu42pZHkWXAXlrow45ZVdo0tRRrVyFyZChhS/lHcp6lqAS8ig+75 prTqTB/Wxf7EfwGvDQL+welu70+KFnnhMG8fmZWwSqpHZZtt+CEbT2R3NGeOgxEZyXFz AOcIKkrY9PdBNNZylN79aONCUvR9yN15tVaY7+xvemvBgZUmutV2ffJbVtGjpqWGqYoJ M83KB0gLnduLJbe3mvBJlbBLqv+3Aw8W1Q6GcaU+p2pWuamsyc+NrwbGZV7SYzSSJ1XF BCCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4XguBKph++G698gHdOWKE+P4P1KjkhqLb40WCE6oSOk=; b=OPgKvboR/TtzhqKQMaXmnHYlGLM040xRgAydODmlRz0fFPsu+SqMJTqHJYJ+eeVRUE 03zHHYbnqy1h2GiYuperff0kJ//8mZnkTs6e7rR58/jKVslGBb8PeU8vepP8OHjjQYTf /ptl1Pnf9loqZ8v1IJULpNmvWHdUKdPnW/k4Oc/8KJ6UU/WzXn2DaWOh612ZccyT/qbQ bqwdmwtL/bdpj19Fdh0vRo3KtZtotEoH/LZsmPWiMhvmx5lsOB5RODxkr3AGdsS6Xbfx Qhf9c6bNMFvjtbjk8BDgcssW/6UFC5HqKyJnYT6mttQ2KMJH0wH3gSrdR51+HOdiVooN PiqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b="VDlCJq/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h4-20020a170906854400b0077b287e3adesi8374327ejy.190.2022.10.17.16.59.30; Mon, 17 Oct 2022 16:59:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b="VDlCJq/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230491AbiJQX6e (ORCPT + 99 others); Mon, 17 Oct 2022 19:58:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230337AbiJQX6P (ORCPT ); Mon, 17 Oct 2022 19:58:15 -0400 Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54ABE7E03A; Mon, 17 Oct 2022 16:58:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1666051070; bh=4XguBKph++G698gHdOWKE+P4P1KjkhqLb40WCE6oSOk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=VDlCJq/sLhkIqnFmAfwgqVdTszKEFM6wd8ROiHLc/tmEMWsfsR5i2aV1AWrVwrI9i T1EzBxnGJTXyvep0yx4y6P/+22fPgV0mOm5EQcHOLc8sRGzrCeOwlAzZhV59OtJKrC nSqkgWBw+Rgi4pS5CATft7rIMF8BgsG4lu+IVWyc= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from Venus.speedport.ip ([84.162.5.241]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MkHMP-1pPv4p2iXB-00kc99; Tue, 18 Oct 2022 01:57:50 +0200 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 v8 01/11] tpm, tpm_tis: Avoid cache incoherency in test for interrupts Date: Tue, 18 Oct 2022 01:57:22 +0200 Message-Id: <20221017235732.10145-2-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221017235732.10145-1-LinoSanfilippo@gmx.de> References: <20221017235732.10145-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:XBj2+8GlMIfJHthnW+YlvZeBjO2uSz7mk6GpwHfOVZW/7hCgP3m M4uJIdXKuNOi7x0advx/i+ZGw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tfcSCygA KbacR14RjLWuBV/YI2Lx2kFvLHhEmyTyI68jXqjsPe3A6kGr9SxmpFiqEaRCx+3+2bZfAabgk +FmwuQmarE4PPFmZAQkqNSEyv5f1t0OcKR+Q96wYRA1cv228AoqJDqSyrlTIMeUJtQlTa/XRo Tt7AFIVRLNbIjMcmaBCJd4wn2ZbEoPgk9RJcwtUPf2rj8ZNNUsSyODDkTlK3PgIdbVlb5VWv8 iA6qnDMHYvWSR6fzsCRg/PjMl0DN0kD4NYM2ufARO9koCYDDH5ed8r1tXpiHHY7eATaFKpBZM aKJAT6rXeA0vfpTtIjaNUI9SHJxyDPrsqGloIX+TIiqEw/6o6jFwW1uCT2srKSVpMQX5uscgE EBth0KgErIA/jO3PwcvEwAKiYKnVjSDg/hYCPgi0YAFRNlF+8ZZ8+VN8n3YNGtLbspJtYcR1I s42DXTqTYZqrdic62fEWPFKRzw4vvZ84uOoGFY96ImeWCXgTX/4jOLgrrV1Xw4PwF7P8lGntu 8Zf1k74TH4E6FM3oeYG3dvnxMzQMCCJbEAx/A3tImOYmPaXW2WV7jMxdU8GhzIvL7TwP4jb/0 1sYe6Bz+zO6Odlp/IbQe9XoUH64ZgJFRBY9GAXEF+/STnB5AmOzMBt+SM87j/fGSNOPVb/qc6 UNJI7DyTiUYxWvuSCbs0EkDFoNbttCxC2a9+veGyjPYj4 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746981297700541303?= X-GMAIL-MSGID: =?utf-8?q?1746981297700541303?= 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;