From: Thomas Fourier <fourier.thomas@gmail.com>
To: Richard Lyu <richard.lyu@suse.com>, Jarkko Sakkinen <jarkko@kernel.org>
Cc: stable@vger.kernel.org, Peter Huewe <peterhuewe@gmx.de>,
Jason Gunthorpe <jgg@ziepe.ca>,
Jerry Snitselaar <jsnitsel@redhat.com>,
"open list:TPM DEVICE DRIVER" <linux-integrity@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] tpm: Fix barriers to prevent hwrng from activating during resume
Date: Tue, 25 Aug 2026 11:42:12 +0200 [thread overview]
Message-ID: <a62fc816-259a-4d28-a265-e425bc17ed50@gmail.com> (raw)
In-Reply-To: <aogdHNl9HLMR89cW@r1chard>
Thank you all for your time and comments. Revisiting my patch after my
vacation, I now think none of the barriers are actually necessary.
On 21/08/2026 11:40, Richard Lyu wrote:
>>> memory reordering between the wake up and clearing the flag is allowed.
>
> When exactly can this reordering happen?
>
>>> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
>>> index 12b7394b34bd..7f500797b7a7 100644 --- a/drivers/char/tpm/tpm-
>>> chip.c
>>> +++ b/drivers/char/tpm/tpm-chip.c @@ -173,6 +173,9 @@ int
>>> tpm_try_get_ops(struct tpm_chip *chip) if (chip->flags &
>>> TPM_CHIP_FLAG_SUSPENDED) goto out_lock;
>>>
>>> + /* Ensure that device is fully resumed */
>>> + rmb();
>>> +
>>> rc = tpm_chip_start(chip);
>>> if (rc)
>>> goto out_lock;
>
> Where inside tpm_chip_start do we actually need to avoid loading a stale
> state or flag?
tpm_chip_start() reads chip->locality for example. I'm not sure it can
be written to concurrently but some drivers write to it. It might not be
a problem as it would just trigger a call to tpm_request_locality().
Since speculative writes are not possible, so rmb() is sufficient; in
any case, no need for a read-to-write memory barrier.
>
>>> diff --git a/drivers/char/tpm/tpm-interface.c
>>> b/drivers/char/tpm/tpm-interface.c index f745a098908b..2de12b02f62b
>>> 100644
>>> --- a/drivers/char/tpm/tpm-interface.c +++
>>> b/drivers/char/tpm/tpm-interface.c @@ -474,13 +474,12 @@ int
>>> tpm_pm_resume(struct device *dev) if (chip == NULL) return -ENODEV;
>>>
>>> - chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED;
>>> -
>>> /*
>>> * Guarantee that SUSPENDED is written last, so that hwrng does not
>>> * activate before the chip has been fully resumed.
>>> */
>>> wmb();
>>> + chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED;
>>
>> Can you rationalize this change?
This change is mostly based on your comment: tpm_pm_resume() is called
after the hardware-specific operations have been performed, and the flag
must be set after these operations are complete.
I assumed hwrng corresponds to tpm_hwrng_read() which calls
tpm_get_random() which itself calls tpm_try_get_ops(). After
tpm_chip_start() is called, tpm_try_get_ops() can return a valid
pointer, and tpm_hwrng_read() can work.
Individual drivers implement the .resume() method by doing
hardware-specific operations then calling tpm_pm_resume(), so ordering
needs to happen between those hardware-specific operations and
tpm_pm_resume(). This ensures that the order of operations is:
- hardware-specific resume operations,
- tpm_pm_resume(), including clearing the TPM_CHIP_FLAG_SUSPENDED flag,
- check that TPM_CHIP_FLAG_SUSPENDED is clear,
- run of tpm_chip_start().
If tpm_try_get_ops() succeeds, then tpm_chip_start() is run so the
hardware-specific resume operations are complete.
>
> I agree the clearing of the flag should be moved after wmb() to
> guarantee that SUSPENDED is written last, the flag has to be cleared
> after the barrier.
> That part makes sense.
>
> My remaining question is whether we actually need the wmb() barrier here?
Revisiting the patch, I agree that this point is not fully clear.
Looking at the implementations of tpm drivers, most use the
tpm_pm_resume() function directly as the .resume() method.
Three drivers implement specific .resume() methods:
- tpm_infineon.c: which uses writeb/outb that adds the necessary
barriers,
- st33zp24/st33zp24.c: which calls tpm_pm_resume() without
driver-specific operation depending on the config,
- tpm_tis_core.c: which makes a self-test before calling tpm_pm_resume().
None of those cases seem to require a barrier at all.
prev parent reply other threads:[~2026-08-25 9:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 9:13 Thomas Fourier
2026-08-03 12:00 ` Jarkko Sakkinen
2026-08-10 15:39 ` Jarkko Sakkinen
2026-08-11 0:03 ` Jarkko Sakkinen
2026-08-21 9:40 ` Richard Lyu
2026-08-25 9:42 ` Thomas Fourier [this message]
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=a62fc816-259a-4d28-a265-e425bc17ed50@gmail.com \
--to=fourier.thomas@gmail.com \
--cc=jarkko@kernel.org \
--cc=jgg@ziepe.ca \
--cc=jsnitsel@redhat.com \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterhuewe@gmx.de \
--cc=richard.lyu@suse.com \
--cc=stable@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®