Skip to content

RFC tpm: Rework timeout and duration handling

Surendran Kanagaraj linux-doc linux-integrity lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 RFC Latest8 patches 7 repliesIn review
  2. v3 RFC8 patches 5 repliesSuperseded

Changes in v4 from the cover letter

- Add a header comment to tpm-limits.c describing the limits table
- Export __tpm_poll() and make tpm_poll_timeout() and tpm_poll_timeout_us() static inline in tpm.h
- Drop tpm_limits_apply_quirks() and struct tpm_limits_quirk
- Keep the existing tpm_tis timeout override table, and set the NitroTPM limits in tpm_crb with tpm_limits_driver_set()
- Drop HW_ERR from the "Adjusting reported" logs
- Document the TPM 2.0 timeouts and durations paths in sysfs-class-tpm

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8tpm: Add a single table for all TPM wait limitsdrivers/char/tpm/Makefile drivers/char/tpm/tpm-chip.c drivers/char/tpm/tpm-limits.c +2 more+414 -05 replies
2/8tpm: Wire the timing setup to the limits tableDocumentation/ABI/stable/sysfs-class-tpm drivers/char/tpm/tpm-limits.c drivers/char/tpm/tpm1-cmd.c +4 more+129 -2000 replies
3/8tpm: Take command durations from the limits tabledrivers/char/tpm/tpm-interface.c drivers/char/tpm/tpm.h drivers/char/tpm/tpm1-cmd.c +1 more+294 -3010 replies
4/8tpm: Bound core and CRB waits by the limits tabledrivers/char/tpm/tpm-interface.c drivers/char/tpm/tpm-limits.c drivers/char/tpm/tpm.h +1 more+87 -612 replies
5/8tpm: Drop the jiffies timing fields from struct tpm_chipdrivers/char/tpm/st33zp24/st33zp24.c drivers/char/tpm/tpm-limits.c drivers/char/tpm/tpm-sysfs.c +9 more+80 -1370 replies
6/8tpm: Add the tpm.limits= parameterDocumentation/admin-guide/kernel-parameters.txt drivers/char/tpm/tpm-limits.c include/linux/tpm.h+114 -60 replies
7/8tpm: Show timeouts and durations for TPM 2.0 chipsDocumentation/ABI/stable/sysfs-class-tpm drivers/char/tpm/tpm-sysfs.c drivers/char/tpm/tpm2-cmd.c+45 -00 replies
8/8tpm_crb: Raise the limits for Amazon NitroTPMdrivers/char/tpm/tpm_crb.c include/linux/tpm.h+38 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [RFC PATCH v4 0/8] tpm: Rework timeout and duration handling

Review of v2 [1] asked to redesign how the timeouts and durations are handled across TPM families and drivers. This series is my attempt at it.

- Interface timeouts live in four fields, TPM1.x duration in an array which uses duration class and TPM2.0 in a different map with raw time values and other duration macros. Move interface timeouts and duration classes to a new struct called struct tpm_limits embedded in struct tpm_chip so that core and most drivers read them from one place.

- Currently it's left up to the driver to initialize the timeout values. During probe, core/driver runs tpm_get_timeouts() which on TPM1.x queries the TPM for timeout values. The transmit function used for this request uses these timeouts for the state changes which can be zero at this time. I found a case with xen-tpmfront where it calls tpm get capability with TIMEOUT_C at zero. Hence the limit table is initialized with default value which is highest among both TPM1.x and TPM2. This makes sure driver is not missing to initialize during probe.

- Add module param option for all duration and timeouts. This gives an option of passing these values via kernel command line.

Current flow:

tpm_chip_alloc() Each driver sets its own default timeouts
or none
tpm1_get_timeouts() reported values, ->update_timeouts()

                               and ->update_durations(), into
                               jiffies fields and chip->duration[]

tpm2_get_timeouts() fixed TPM2_TIMEOUT_A..D, no driver hook
tpm_calc_ordinal_duration() chip->duration[] on TPM 1.x, a fixed
per-ordinal map on TPM 2.0
tpm_transmit() resubmits on TPM2_RC_RETRY until the
delay passes TPM2_DURATION_LONG

The limit table is built in fixed order starting with family defaults, reported by TPM chip, ->adjust_limits, tpm.limits= parameter and finally the validation that checks the bounds. This also adds a visibility to the source which sets the limit eg: default, TPM, driver, param etc.

The flow after this series:

tpm_chip_alloc()
tpm_limits_init() larger of the TPM 1.x and 2.0 defaults,
tpm.limits= and bounds check
tpm1_get_timeouts(), tpm2_get_timeouts()
tpm_limits_update() family defaults
reported values (TPM 1.x) ->adjust_limits() tpm.limits=
bounds check
readers of chip->limits
tpm_try_transmit() polls for completion up to the command's
duration class
tpm_transmit() resubmits on TPM2_RC_RETRY up to the
LONG duration
drivers tpm_poll_timeout(), or the limit in
jiffies where a driver counts jiffies
sysfs timeouts and durations, on TPM 2.0 too

This changes some of the existing behaviour:

- TPM2_VerifySignature waits DEFAULT 120 s instead of 30 s.
- Completion polling first sleeps at most 100 us instead of 1 ms.
- Until the timing query runs, tpm_crb waits up to 750 ms for TIMEOUT_C instead of 200 ms, xen-tpmfront 750 ms instead of 0, and st33zp24, tpm_i2c_infineon and tpm_i2c_nuvoton up to 4 s for TIMEOUT_B instead of 2 s.
- On TPM 1.x, an ordinal with no class waits DEFAULT, which is never shorter than LONG, so a chip that reports LONG above two minutes waits longer than the old fixed two minutes.
- The timeouts and durations sysfs files now print exact microseconds, and their "[adjusted]" marker also covers a value changed by validation. TPM 2.0 chips now get both files too, in /sys/class/tpm/tpmX/. tpm.limits= is documented in kernel-parameters.txt.
- tpm_atmel, tpm_nsc and tpm_infineon never query the TPM for its timeouts and durations, so their sysfs files now show the defaults, marked "[original]", instead of zero timeouts and an empty durations file.

I ran each patch in QEMU with swtpm, on TPM 2.0 over CRB and TIS and on TPM 1.2 over TIS. Every boot answered commands and the TPM 1.2 sysfs output stays the same across the series. I also fed tpm.limits= values below the floor, above the ceiling and malformed, and with a local test hook held the TPM in TPM2_RC_RETRY and stalled completions to see each wait end where the table says.

On an EC2 instance with Amazon NitroTPM, I ran a GetCapability and hwrng loop through repeated periods of TPM unavailability. Kernel without these changes logged "tpm2_load_context: failed with a TPM error 0x0922" and every later TPM command failed. With this series applied, no command failed.

The idea for this series is mine. An LLM coding assistant helped draft the code and I reviewed every patch myself.

[1] https://lore.kernel.org/linux-integrity/[email protected]/

Surendran

V3 -> V4:
- Add a header comment to tpm-limits.c describing the limits table
- Export __tpm_poll() and make tpm_poll_timeout() and tpm_poll_timeout_us() static inline in tpm.h
- Drop tpm_limits_apply_quirks() and struct tpm_limits_quirk
- Keep the existing tpm_tis timeout override table, and set the NitroTPM limits in tpm_crb with tpm_limits_driver_set()
- Drop HW_ERR from the "Adjusting reported" logs
- Document the TPM 2.0 timeouts and durations paths in sysfs-class-tpm

V2 -> V3:
- Replace the TPM 2.0 duration override with one limits table for both TPM families, built in a fixed order and validated
- Replace ->update_timeouts() and ->update_durations() with ->adjust_limits()
- Bound the tpm_transmit() retry loop by the LONG duration instead of TPM2_DURATION_LONG
- Add tpm.limits=, and show the timeouts and durations files for TPM 2.0 chips
- Move the NitroTPM values to a tpm_crb quirk table, raise its SHORT and MEDIUM durations to 30 s and its LONG duration to 45 s

V1 -> V2:
- Drop chip->busy_timeout_ms. TPM2 durations and the TPM2_RC_RETRY budget now come from chip->duration.
- Map TPM2_CC_SELF_TEST and TPM2_CC_VERIFY_SIGNATURE to TPM_LONG with floors of 3s and 30s since their durations do not fit SHORT, MEDIUM, LONG durations.
- Keep the TPM2 defaults for commands sent before tpm2_get_timeouts() has run such as the one in tpm2_probe() and keep the 2s retry budget for TPM 1.x chips.
- Keep the first TPM2_RC_RETRY wait at TPM2_DURATION_SHORT instead of chip->duration[TPM_SHORT]. No TPM2 command maps to TPM_SHORT today and a driver that raises TPM_SHORT for its commands should not also lengthen the first retry wait.
- Return early from tpm_tis_update_durations() for TPM2 chips, since its overrides are for TPM 1.x.
- Raise TPM_SHORT, TPM_MEDIUM and TPM_LONG to 30s for NitroTPM.
- Replace the vendor quirk table in tpm_crb with a plain vendor ID check.
- Use the TPM_CRB_ prefix for the new tpm_crb macros.
- Rename the patches from "tpm: Add per-chip timeout for transient unavailability" and "tpm_crb: Raise timeouts for Amazon NitroTPM".

v3: https://lore.kernel.org/linux-integrity/[email protected]/
v2: https://lore.kernel.org/linux-integrity/[email protected]/
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Surendran Kanagaraj (8):
tpm: Add a single table for all TPM wait limits
tpm: Wire the timing setup to the limits table
tpm: Take command durations from the limits table
tpm: Bound core and CRB waits by the limits table
tpm: Drop the jiffies timing fields from struct tpm_chip
tpm: Add the tpm.limits= parameter
tpm: Show timeouts and durations for TPM 2.0 chips
tpm_crb: Raise the limits for Amazon NitroTPM

 Documentation/ABI/stable/sysfs-class-tpm      |  38 +-
 .../admin-guide/kernel-parameters.txt         |  13 +
 drivers/char/tpm/Makefile                     |   1 +
 drivers/char/tpm/st33zp24/st33zp24.c          |  28 +-
 drivers/char/tpm/tpm-chip.c                   |   1 +
 drivers/char/tpm/tpm-interface.c              |  72 +-
 drivers/char/tpm/tpm-limits.c                 | 427 +++++++++++
 drivers/char/tpm/tpm-sysfs.c                  |  29 +-
 drivers/char/tpm/tpm.h                        |  63 +-
 drivers/char/tpm/tpm1-cmd.c                   | 669 +++++++++---------
 drivers/char/tpm/tpm2-cmd.c                   |  80 ++-
 drivers/char/tpm/tpm_crb.c                    | 107 ++-
 drivers/char/tpm/tpm_i2c_atmel.c              |   9 -
 drivers/char/tpm/tpm_i2c_infineon.c           |  25 +-
 drivers/char/tpm/tpm_i2c_nuvoton.c            |  32 +-
 drivers/char/tpm/tpm_tis_core.c               | 168 ++---
 drivers/char/tpm/tpm_tis_i2c_cr50.c           |  15 +-
 drivers/char/tpm/xen-tpmfront.c               |  10 +-
 include/linux/tpm.h                           |  66 +-
 include/linux/tpm_ptp.h                       |   9 -
 20 files changed, 1179 insertions(+), 683 deletions(-)
 create mode 100644 drivers/char/tpm/tpm-limits.c

base-commit: 8054cba7f0619486c237b21bd257a4544b5de455

Signature · 1 line
2.47.3