From patchwork Thu Nov 24 13:55: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: 25549 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3411069wrr; Thu, 24 Nov 2022 05:58:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf42f0Rl/sapBu3Vx9tj1Buc9UPjP3n6zK4AaYhcY1cpSb/4BXsE5tLSu/bPC5WJrJ2obJ9f X-Received: by 2002:a05:6402:1814:b0:469:d569:a478 with SMTP id g20-20020a056402181400b00469d569a478mr13649436edy.369.1669298308438; Thu, 24 Nov 2022 05:58:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669298308; cv=none; d=google.com; s=arc-20160816; b=TUMeiZM8hzPUt0KkQCX5ARyEjriL1mdQCAPWLNv4nXTOGC1Yt5x4d7WqtsFaYo4/JU Q5i0H6P+R0x++YQyaXTQmddTOlieFbm13Seko1TADOM47bMfI9BciNCFrMw9QtCU9g3H HvrXc7/HKrJrUqYgmk8ieIUZfZ8rhkECcpKGxhPYRunb0YEBGFjDyT/LYqWtI1St/UJ7 PLm32HxFlvvTo+AKz+3ArK16u18JQTJvgRzHb1DQOkxHEDC3i5d3b7QCqrRgxOdO0zn/ qzFUHqCwEuwkwYfN5Nc8g0iYw7BYFL0ZQPnNURlRongOvPP3e4NX+Ru3pvM+BMWINn1V cg8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:ui-outboundreport:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=A4TNiZ1MIqgxBlywM7kePzrqXJ+tGCscjealDs3aYxw=; b=O3hPngwKvnADMqJEop2ssddB6++YvyJzeV6Pzvii2HbOXi38Zwo573iYbGFIIQdCiB 00Sa+yMvxGrolhL9SgchIsoi7HUPT/F+vY/bpPeuCrQfj/jTRaORUqqTtbAfHYxH07zo 6giJ2qQpdXzqdDoFOL0d0FbhY1x+jF/gdvKQ/Oqnwf2pgHWQVkPIiHCFgP4+EWUPHy/S JT5CMJRvNgyIQPw46Rfsb/qz0PXdmkspp5tVV+z51JkWkypnA4pe41B30U4KmNwNHK1m IRV9bNR4Pvf1nWWEdR1zIcG7xyiXttKv5pCsewmy7AfjhqxT+lV0qfzEyelLzdJg3Qze nOeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=hagk1JxJ; 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=pass (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 xh13-20020a170906da8d00b007b8b7da6490si966197ejb.660.2022.11.24.05.58.05; Thu, 24 Nov 2022 05:58:28 -0800 (PST) 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.de header.s=s31663417 header.b=hagk1JxJ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230326AbiKXN5S (ORCPT + 99 others); Thu, 24 Nov 2022 08:57:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230138AbiKXN4g (ORCPT ); Thu, 24 Nov 2022 08:56:36 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12D48116067; Thu, 24 Nov 2022 05:56:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1669298170; bh=A4TNiZ1MIqgxBlywM7kePzrqXJ+tGCscjealDs3aYxw=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=hagk1JxJE4VdMc0GZGSSvhM4IGCJC8Jth77FtedhS4ETNsascWJ5pRIexQybW+tKe oELxZe9XBBWMwFY8ib3l+KHru9F2/28kxgJ4K6Y3oOq4VCa/LZXm/LIYR9WjqBHThf MQUhtojT9Udarjn34rXeQsFpGQHBnIwUTHC8GeAkz+/4ziJzDxtKRGTzqUyoqHMGsB DEuzjN7QuOHcNWJylfvuiCfmtTj2E3Cu96MoA27cuw7jd/boQ1Ay7mkBso6lA7F/sB P+oOfzptf5bRPxFWwD/DPu8e9LhNLATRBaYLhyBGfoiTQqcPkfpp9r/B4bZFe/VpCm 0DNlajCZ7lFQw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from Venus.speedport.ip ([84.162.7.17]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MjS9I-1pLxfE1AEc-00kulG; Thu, 24 Nov 2022 14:56:10 +0100 From: Lino Sanfilippo To: peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca Cc: stefanb@linux.vnet.ibm.com, linux@mniewoehner.de, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, jandryuk@gmail.com, pmenzel@molgen.mpg.de, l.sanfilippo@kunbus.com, LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com Subject: [PATCH v11 01/14] tpm, tpm_tis: Avoid cache incoherency in test for interrupts Date: Thu, 24 Nov 2022 14:55:25 +0100 Message-Id: <20221124135538.31020-2-LinoSanfilippo@gmx.de> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20221124135538.31020-1-LinoSanfilippo@gmx.de> References: <20221124135538.31020-1-LinoSanfilippo@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:MQQR6Jun0BPLpfZQKEpXcjYTEKKRsXbOtOxBKv5g8XyUjQMxeL1 wdCg2l+1GnKsuAMeRdZ9RMLr663bQDOqDCZdUT3Q5n5ssMDs8rJItbYlVwk8bvokECh0VpJ 8RB4mfmRHGFf9B4Z39gwFECsuwY2YUMJZP/c/znwPoaPOepJ09OhoBSq6P6Uw+W30ev3DCO gjDdjHI/G7N/l2dN6P/YQ== UI-OutboundReport: notjunk:1;M01:P0:pkzNZjHCOV4=;khFbfQ6MIG0InRJW8D6vRzSBJKd +BFcnCShZ55mzyUCUh3BauK0F6RF3Lv3M1d7fSqovH7+O9NUX6EM82AsIxIywJ7h1ze3lRYNk z9V63qZF3gj2d82fF5ehzadQVk2uE3CcHYRmpLVfm3FQY2YcI0mhmZ86ViFf75CqtFIOPRV1R rW42HTswseB+wF9mZQeY1RgMj6IW2WTAKB66itvrp2YZwtQHaylwMpYB5m7nIQxr/BM66MGuY h46k/VPVUcMuUN5VuzrpsFvfLlaAe9UdYVEehRnMFhoHyIF83yKptelszm+zIpHxV6/D7vhCt JPbv946ul5kDsZaPE5HLAgbXgngLjUCtqGv5nmuQ09tVDad/+nb/K13p9Ba4eQg6hgmU/DlZx 6EZAEKqJL6ETQLRFym2EsuvAx/bBHtbmtMrcQQ6vu1vqYBXl1oFbeuQ1L8397NKXFnc7A8iSz GoGa8aWzzCoocVyTLbU3ORTfSIfiUmBC3DYgJZR7t59jA1mWUM2KiolRegcQvDSZlweNaMUC2 /7CAZgnagdIaKPyttuCgTNh5wHpeqTMDKxZdhBjCqDe6+FkcLylAlSpaxqHn/RRTID8GaeNEq OvYq9giFrrTO/h7z3+/qeNSEci7MxwJTJvswNiQSMEapWxtA6F7yd8vVNs4TK8bscTVDBFd6T nr2L6QZZTkpYzsTWVRXMxuzW6lfFi6evdoYCOVHFxSmL0RaaWhYF7Lc2vhUfWTd+ZUnXGWlnC zqIc1aG6et6OVdPw1jk1k+sYyOMjQDkrx3qFwus1/Z8tfRqvthoHSFoSDwgfpPNZ/eY8Gjzt4 VZaL1k5V29raudesjEIRppLOiO4slZtzJmgFDGfUN91DCnORN5bSP6jK4V62jXNmVfuTMy41R F1YPfZv2V/r2GG2ytN4s1PaEdl365uikll3yaqVRS5VgDEv6WleG4jjM7o9rT6n7qtj8V+8oy O3oUng== X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,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?1750386143155620434?= X-GMAIL-MSGID: =?utf-8?q?1750386143155620434?= 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 tpm_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;