tpm: Check for completion after timeout
authorJarkko Sakkinen <jarkko@kernel.org>
Tue, 22 Jul 2025 23:21:24 +0000 (02:21 +0300)
committerJarkko Sakkinen <jarkko@kernel.org>
Tue, 22 Jul 2025 23:23:18 +0000 (02:23 +0300)
commitd4640c394f23b202a89512346cf28f6622a49031
treed617a19b643c4b265240df8921ec51342a064fdc
parent63d1dbfef6ac5ae92c672a633e4b38998dfa03b2
tpm: Check for completion after timeout

The current implementation of timeout detection works in the following
way:

1. Read completion status. If completed, return the data
2. Sleep for some time (usleep_range)
3. Check for timeout using current jiffies value. Return an error if
   timed out
4. Goto 1

usleep_range doesn't guarantee it's always going to wake up strictly in
(min, max) range, so such a situation is possible:

1. Driver reads completion status. No completion yet
2. Process sleeps indefinitely. In the meantime, TPM responds
3. We check for timeout without checking for the completion again.
   Result is lost.

Such a situation also happens for the guest VMs: if vCPU goes to sleep
and doesn't get scheduled for some time, the guest TPM driver will
timeout instantly after waking up without checking for the completion
(which may already be in place).

Perform the completion check once again after exiting the busy loop in
order to give the device the last chance to send us some data.

Since now we check for completion in two places, extract this check into
a separate function.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Jonathan McDowell <noodles@meta.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
drivers/char/tpm/tpm-interface.c