Skip to content

Reuse transport connections and reduce NV write and hash cache overhead - #563

Open
aidangarske wants to merge 5 commits into
wolfSSL:masterfrom
aidangarske:fenrir-fixes-7606-7610
Open

Reuse transport connections and reduce NV write and hash cache overhead#563
aidangarske wants to merge 5 commits into
wolfSSL:masterfrom
aidangarske:fenrir-fixes-7606-7610

Conversation

@aidangarske

Copy link
Copy Markdown
Member

Description

F-7606 commit aidangarske@5147503 - Every chunk of a chunked NV write re-read the NV public area (a full TPM round trip) to recompute the index name, but the name only changes when the first write sets TPMA_NV_WRITTEN. The write loop now re-opens only until the written bit is reflected in the cached name (and always for policy sessions), so a chunked write costs at most 2 NV_ReadPublic instead of one per chunk and rewrites cost none. Proven by a new 3-chunk write plus rewrite test under an HMAC parameter encryption session, where a stale name would fail the session HMAC.

F-7607 commit aidangarske@b7dc3ba - The TPM crypto callback hash cache grew 256 bytes at a time, so streaming small updates copied O(n²) bytes: a 16 KB transcript took 64 reallocations copying 516 KB. It now doubles on growth, dropping that to 7 allocations and 16 KB copied (at 1 MB, under 1 ms of copying instead of about 150 ms) for up to 2x transient capacity overshoot. A new test streams SHA-256 through the cache in tiny updates and matches a software digest.

F-7608 commit aidangarske@7fd8b6c - The Linux SPI HAL did open, three config ioctls, transfer, close around every TIS register access: six syscalls per 1 byte status poll. The descriptor is now cached (O_CLOEXEC) with SPI mode set once at open and speed/word size passed per transfer; transfer failures and autodetect probing drop the descriptor so the next access reopens. Also fixes an int pointer passed to the u8 bits-per-word ioctl. Compile verified in six Linux configs; no SPI hardware was attached, so hardware gains are projected.

F-7609 commit aidangarske@26f97aa - The swtpm socket transport did getaddrinfo, connect, and close for every TPM command. The TCP connection now persists like the UART path: closed in TPM2_Cleanup (TPM2_SwtpmCloseUART generalized to TPM2_SwtpmClose), on any transport failure, and on a failed init, with FD_CLOEXEC, SIGPIPE suppression, and TCP_NODELAY since it is long lived. Measured on identical builds: 501 to 33 connections per unit suite run and suite wall clock 20.7 s to 14.1 s, about 1.5x; also fixed a unit test that leaked a live connection by re-initializing without cleanup. Hardware TPMs do not use this transport and are unaffected.

F-7610 commit aidangarske@9a5c01b - The Linux I2C HAL had the same open/close around every register access; same fix as SPI (cached O_CLOEXEC descriptor, reopen after failure), with the existing busy retry loops untouched. I2C bus transactions dwarf the open cost, so this is syscall hygiene more than wall clock. Compile verified in both I2C configs.

Copilot AI lite review requested due to automatic review settings August 4, 2026 19:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes wolfTPM’s host-side performance by reducing per-command/per-chunk transport and TPM round trips, improving streaming hash-cache growth behavior, and adding regression tests to validate the new behavior across swtpm transport and NV write flows.

Changes:

  • Reuse swtpm TCP connections across commands and close persistent swtpm UART/socket connections during TPM2_Cleanup / early-init failures.
  • Reduce NV chunked write overhead by avoiding redundant NV_ReadPublic/name recomputation once TPMA_NV_WRITTEN is reflected in the cached NV name (with a new multi-chunk regression test).
  • Improve crypto callback hash-cache growth from fixed increments to a doubling strategy (with a new streaming hash-cache test).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
wolftpm/tpm2_swtpm.h Rename/generalize swtpm close helper to cover UART and socket.
src/tpm2.c Initialize swtpm fd safely and close persistent swtpm connection during cleanup.
src/tpm2_wrap.c Ensure swtpm connections are closed on early init/test failures.
src/tpm2_swtpm.c Persist swtpm socket connection; add CLOEXEC, SIGPIPE suppression, TCP_NODELAY; close-on-failure behavior.
src/tpm2_wrap.c Reduce redundant NV public/name recomputation during chunked NV writes.
src/tpm2_cryptocb.c Switch hash cache growth to doubling (performance optimization).
hal/tpm_io_linux.c Cache Linux SPI/I2C device fds to avoid open/close per register access; set per-transfer speed/bpw.
tests/unit_tests.c Add NV chunked write+rewrite test and hash-cache streaming test; ensure cleanup in init test.
examples/tpm_test.h Add a dedicated NV index constant for chunked-write testing.
docs/SWTPM.md Document persistent connection behavior and cleanup semantics for swtpm socket/UART.

Comment thread src/tpm2_cryptocb.c
@aidangarske aidangarske self-assigned this Aug 4, 2026
@aidangarske
aidangarske force-pushed the fenrir-fixes-7606-7610 branch from 9a5c01b to 8030099 Compare August 4, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants