All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lino Sanfilippo <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,
	LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com,
	Lino Sanfilippo <l.sanfilippo@kunbus.com>
Subject: [PATCH v4 4/6] tpm, tpm_tis: avoid CPU cache incoherency in irq test
Date: Mon,  9 May 2022 10:05:57 +0200	[thread overview]
Message-ID: <20220509080559.4381-5-LinoSanfilippo@gmx.de> (raw)
In-Reply-To: <20220509080559.4381-1-LinoSanfilippo@gmx.de>

From: Lino Sanfilippo <l.sanfilippo@kunbus.com>

The interrupt handler that sets irq_tested to indicate that interrupts are
working may run on another CPU than the thread that checks this variable in
tmp_tis_send().

Since no synchronization is used to access irq_tested, there is no
guarantee for cache coherency between the CPUs, so that the value set by
the interrupt handler might not be visible to the testing thread.

Avoid this issue by using a bitfield instead of a boolean variable and by
accessing this field with bit manipulating functions that guarantee cache
coherency.

Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 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 4f3b82c3f205..bdfde1cd71fe 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_IRQTEST_OK, &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_IRQTEST_OK, &priv->irqtest_flags))
 		tpm_msleep(1);
-	if (!priv->irq_tested)
+	if (!test_bit(TPM_TIS_IRQTEST_OK, &priv->irqtest_flags))
 		disable_interrupts(chip);
-	priv->irq_tested = true;
+	set_bit(TPM_TIS_IRQTEST_OK, &priv->irqtest_flags);
 	return rc;
 }
 
@@ -689,7 +690,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_IRQTEST_OK, &priv->irqtest_flags);
 	if (interrupt & TPM_INTF_DATA_AVAIL_INT)
 		wake_up_interruptible(&priv->read_queue);
 	if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
@@ -780,7 +781,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_IRQTEST_OK, &priv->irqtest_flags);
 	chip->flags |= TPM_CHIP_FLAG_IRQ;
 
 	/* Generate an interrupt by having the core call through to
diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h
index 43b724e55192..c8972ea8e13e 100644
--- a/drivers/char/tpm/tpm_tis_core.h
+++ b/drivers/char/tpm/tpm_tis_core.h
@@ -89,11 +89,15 @@ enum tpm_tis_flags {
 	TPM_TIS_USE_THREADED_IRQ	= BIT(2),
 };
 
+enum tpm_tis_irqtest_flags {
+	TPM_TIS_IRQTEST_OK		= 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;
-- 
2.36.0


  parent reply	other threads:[~2022-05-09  8:29 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-09  8:05 [PATCH v4 0/6] TPM irq fixes Lino Sanfilippo
2022-05-09  8:05 ` [PATCH v4 1/6] tpm, tpm_tis_spi: Request threaded irq Lino Sanfilippo
2022-05-11 11:22   ` Jarkko Sakkinen
2022-05-11 19:18     ` Lino Sanfilippo
2022-05-13 18:08       ` Jarkko Sakkinen
2022-05-16 19:52         ` Lino Sanfilippo
2022-05-17 18:23           ` Jarkko Sakkinen
2022-05-09  8:05 ` [PATCH v4 2/6] tpm, tpm_tis: Claim and release locality only once Lino Sanfilippo
2022-05-11 11:27   ` Jarkko Sakkinen
2022-05-11 19:29     ` Lino Sanfilippo
2022-05-13 17:59       ` Jarkko Sakkinen
2022-05-16 20:23         ` Lino Sanfilippo
2022-05-09  8:05 ` [PATCH v4 3/6] tpm, tpm_tis: enable irq test Lino Sanfilippo
2022-05-09  8:05 ` Lino Sanfilippo [this message]
2022-05-11 15:06   ` [PATCH v4 4/6] tpm, tpm_tis: avoid CPU cache incoherency in " Jarkko Sakkinen
2022-05-11 19:35     ` Lino Sanfilippo
2022-05-09  8:05 ` [PATCH v4 5/6] tpm, tpm_tis: Move irq test from tpm_tis_send() to tpm_tis_probe_irq_single() Lino Sanfilippo
2022-05-11 15:09   ` Jarkko Sakkinen
2022-05-11 19:56     ` Lino Sanfilippo
2022-05-16 17:51       ` Jarkko Sakkinen
2022-05-16 20:25         ` Lino Sanfilippo
2022-05-17 18:19           ` Michael Niewöhner
2022-05-18  1:26             ` Jarkko Sakkinen
2022-05-18 16:08               ` Michael Niewöhner
2022-05-09  8:05 ` [PATCH v4 6/6] tpm, tpm_tis: Only enable supported IRQs Lino Sanfilippo
2022-05-11 15:08   ` Jarkko Sakkinen
2022-05-11 19:58     ` Lino Sanfilippo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220509080559.4381-5-LinoSanfilippo@gmx.de \
    --to=linosanfilippo@gmx.de \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=l.sanfilippo@kunbus.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@mniewoehner.de \
    --cc=lukas@wunner.de \
    --cc=p.rosenberger@kunbus.com \
    --cc=peterhuewe@gmx.de \
    --cc=stefanb@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.