On Wed, Nov 25, 2026 at 01:54:29PM +0700, Rustam Mustafin wrote: > Test that IORING_OP_READ on /dev/tpm0 and /dev/tpmrm0 fails with -EIO and > that the following read() returns the whole response. The test needs > liburing-ffi (liburing 2.4+) and is skipped without it. > > Signed-off-by: Rustam Mustafin <[email protected]> > --- > > tools/testing/selftests/tpm2/Makefile | 2 +- > tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ > tools/testing/selftests/tpm2/tpm2_tests.py | 72 ++++++++++++++++++++++ > 3 files changed, 88 insertions(+), 1 deletion(-) > create mode 100755 tools/testing/selftests/tpm2/test_uring.sh > > diff --git a/tools/testing/selftests/tpm2/Makefile > b/tools/testing/selftests/tpm2/Makefile > index a9bf9459fb25..44630fb980fa 100644 > --- a/tools/testing/selftests/tpm2/Makefile > +++ b/tools/testing/selftests/tpm2/Makefile > @@ -1,5 +1,5 @@ > # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) > include ../lib.mk > > -TEST_PROGS := test_smoke.sh test_space.sh test_async.sh > +TEST_PROGS := test_smoke.sh test_space.sh test_async.sh test_uring.sh > TEST_PROGS_EXTENDED := tpm2.py tpm2_tests.py > diff --git a/tools/testing/selftests/tpm2/test_uring.sh > b/tools/testing/selftests/tpm2/test_uring.sh > new file mode 100755 > index 000000000000..516985ef20af > --- /dev/null > +++ b/tools/testing/selftests/tpm2/test_uring.sh > @@ -0,0 +1,15 @@ > +#!/bin/sh > +# SPDX-License-Identifier: GPL-2.0-only > + > +# Kselftest framework requirement - SKIP code is 4. > +ksft_skip=4 > + > +[ -e /dev/tpm0 ] || exit $ksft_skip > +[ -e /dev/tpmrm0 ] || exit $ksft_skip > +read tpm_version < /sys/class/tpm/tpm0/tpm_version_major > +[ "$tpm_version" = 2 ] || exit $ksft_skip > + > +python3 -c 'import sys, tpm2_tests > +sys.exit(not tpm2_tests.uring_available())' || exit $ksft_skip > + > +python3 -m unittest -v tpm2_tests.UringTest 2>&1 > diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py > b/tools/testing/selftests/tpm2/tpm2_tests.py > index ffe98b5c8d22..6fe60e00ff06 100644 > --- a/tools/testing/selftests/tpm2/tpm2_tests.py > +++ b/tools/testing/selftests/tpm2/tpm2_tests.py > @@ -9,6 +9,7 @@ from tpm2 import ProtocolError > import unittest > import logging > import struct > +import errno > > class SmokeTest(unittest.TestCase): > def setUp(self): > @@ -331,3 +332,74 @@ class AsyncTest(unittest.TestCase): > > self.assertEqual(rc, 22) > async_client.close() > + > +def uring_available(): > + import ctypes.util > + > + name = ctypes.util.find_library('uring-ffi') > + if not name: > + return False > + lib = ctypes.CDLL(name) > + ring = ctypes.create_string_buffer(1024) > + if lib.io_uring_queue_init(1, ring, 0) < 0: > + return False > + lib.io_uring_queue_exit(ring) > + return True > + > +def uring_read(fd, size): > + import ctypes.util > + > + lib = ctypes.CDLL(ctypes.util.find_library('uring-ffi')) > + lib.io_uring_get_sqe.restype = ctypes.c_void_p > + lib.io_uring_prep_read.argtypes = [ctypes.c_void_p, ctypes.c_int, > + ctypes.c_void_p, ctypes.c_uint, > + ctypes.c_uint64] > + ring = ctypes.create_string_buffer(1024) > + ret = lib.io_uring_queue_init(1, ring, 0) > + if ret < 0: > + raise OSError(-ret, 'io_uring_queue_init') > + try: > + buf = ctypes.create_string_buffer(size) > + lib.io_uring_prep_read(lib.io_uring_get_sqe(ring), fd, buf, size, 0) > + ret = lib.io_uring_submit(ring) > + if ret < 0: > + raise OSError(-ret, 'io_uring_submit') > + cqe = ctypes.c_void_p() > + ret = lib.io_uring_wait_cqe(ring, ctypes.byref(cqe)) > + if ret < 0: > + raise OSError(-ret, 'io_uring_wait_cqe') > + # cqe->res follows the 64-bit user_data > + res = ctypes.c_int.from_address(cqe.value + 8).value > + finally: > + lib.io_uring_queue_exit(ring) > + > + return res > + > +class UringTest(unittest.TestCase): > + def read_response(self, flags): > + client = tpm2.Client(flags) > + fmt = '>HII III' > + cmd = struct.pack(fmt, > + tpm2.TPM2_ST_NO_SESSIONS, > + struct.calcsize(fmt), > + tpm2.TPM2_CC_GET_CAPABILITY, > + tpm2.TPM2_CAP_TPM_PROPERTIES, > + tpm2.TPM2_PT_FIXED, 1) > + try: > + client.tpm.write(cmd) > + res = uring_read(client.tpm.fileno(), 4096) > + # The rejected read must leave the whole response in place > + rsp = client.tpm.read(4096) > + finally: > + client.close() > + > + self.assertEqual(res, -errno.EIO) > + sz, rc = struct.unpack('>II', rsp[2:10]) > + self.assertEqual(sz, len(rsp)) > + self.assertEqual(rc, 0) > + > + def test_read_tpm(self): > + self.read_response(0) > + > + def test_read_tpmrm(self): > + self.read_response(tpm2.Client.FLAG_SPACE) > -- > 2.52.0 >
Thanks this much better. Reviewed-by: Jarkko Sakkinen <[email protected]> Br, Jarkko
