Skip to contentSkip to conversation

tsm-mr: Unified Measurement Register ABI for TVMs

PATCH Superseded Cedric Xing 3 participants 12 messages Apr 24, 2025 – May 1, 2025

Subsystems (5)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover tsm-mr: Unified Measurement Register ABI for TVMs

NOTE: This patch series introduces the Measurement Register (MR) ABI, and is a continuation of the RFC series on the same topic [1].

Introduce the CONFIG_TSM_MEASUREMENTS helper library (tsm-mr) as a cross-vendor transport schema to allow TVM (TEE VM) guest drivers to export CC (Confidential Compute) architecture-specific MRs (Measurement Registers) as sysfs attributes/files. Enable applications to read, write/extend MRs like regular files, supporting various usages such as configuration verification (e.g., verify a TVM's configuration against digests stored in static/immutable MRs like MRCONFIGID on TDX or HOSTDATA on SEV) and runtime measurements (e.g., extend the measurement of a container image to an RTMR before running it).

Patches included in this series:

- Patch 1 adds the tsm-mr library to help TVM guest drivers exposing MRs as sysfs attributes.
- Patch 2 provides a sample module demonstrating the usage of the new tsm-mr library.
- The remaining patches update the TDX guest driver to expose TDX MRs with the help of the tsm-mr library.

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

---
Changes in v5:
- tsm-mr: Fix typo in MAINTAINERS.
- tsm-mr-sample: Fix Kconfig dependencies by adding select VIRT_DRIVERS.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- tsm-mr: Add details to ABI doc.
- tsm-mr: Add driver-api doc.
- tsm-mr: Add tracepoints.
- tsm-mr: Use the constant string "mr" as the group/directory name for MR sysfs attributes.
- x86/tdx: Revise error codes returned by tdx_mcall_* functions.
- tdx-guest: Merge all TDREPORT code paths into a common function.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- tsm-mr: Separate measurement support (tsm-mr) from the original tsm source code. Modules depending on tsm-mr should select TSM_MEASUREMENTS in Kconfig. (Dan Williams)
- tsm-mr: Revise tsm-mr APIs to allow callers to decide where to host the MR attributes in sysfs.
- tsm-mr: Drop TSM_MR_F_EXTENSIBLE and route all "write" requests to the CC guest driver, which decides how to handle writes (e.g., by extending the buffer on input to the specified MR).
- tsm-mr: Update the naming pattern for MR attributes from MRNAME/HASH/digest to MRNAME:HASH.
- tsm-mr: Drop TSM_MR_MAXBANKS kernel config.
- x86/tdx: Return -EBUSY from tdx_mcall_get_report0 on TDCALL_OPERAND_BUSY error.
- tdx-guest: Move MR attributes from /sys/kernel/tsm/tdx/ to /sys/class/misc/tdx_guest/ because MR names are architecture-specific, so their attributes should reside in an architecture-specific location.
- tdx-guest: Remove hash from mrconfigid, mrowner, mrownerconfig.
- tdx-guest: Remove servtd_hash, report0, and reportdata.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Added TSM_MR_MAXBANKS Kconfig option
- Updated Kconfig dependency for TSM_REPORTS
- Updated comments in include/linux/tsm.h
- Updated drivers/virt/coco/tdx-guest/tdx-guest.c to use IS_BUILTIN() for determining if static buffer addresses can be converted to GPAs by virt_to_phys()
- Renamed function tdx_mcall_rtmr_extend() -> tdx_mcall_extend_rtmr()
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Cedric Xing (5):
tsm-mr: Add TVM Measurement Register support
tsm-mr: Add tsm-mr sample code
x86/tdx: Add tdx_mcall_extend_rtmr() interface
x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error
virt: tdx-guest: Expose TDX MRs as sysfs attributes

 .../testing/sysfs-devices-virtual-misc-tdx_guest   |  60 +++++
 Documentation/driver-api/coco/index.rst            |  12 +
 .../driver-api/coco/measurement-registers.rst      |  12 +
 Documentation/driver-api/index.rst                 |   1 +
 MAINTAINERS                                        |   6 +-
 arch/x86/coco/tdx/tdx.c                            |  50 +++-
 arch/x86/include/asm/shared/tdx.h                  |   1 +
 arch/x86/include/asm/tdx.h                         |   2 +
 drivers/virt/coco/Kconfig                          |   5 +
 drivers/virt/coco/Makefile                         |   1 +
 drivers/virt/coco/tdx-guest/Kconfig                |   1 +
 drivers/virt/coco/tdx-guest/tdx-guest.c            | 291 +++++++++++++--------
 drivers/virt/coco/tsm-mr.c                         | 247 +++++++++++++++++
 include/linux/tsm-mr.h                             |  94 +++++++
 include/trace/events/tsm_mr.h                      |  80 ++++++
 samples/Kconfig                                    |  11 +
 samples/Makefile                                   |   1 +
 samples/tsm-mr/Makefile                            |   2 +
 samples/tsm-mr/tsm_mr_sample.c                     | 137 ++++++++++
 19 files changed, 904 insertions(+), 110 deletions(-)

---
base-commit: 9c32cda43eb78f78c73aee4aa344b777714e259b
change-id: 20250209-tdx-rtmr-255479667146

Best regards,

Signature · 1 line
Cedric Xing <[email protected]>
#1 lore raw

PATCH v5 1/5 tsm-mr: Add TVM Measurement Register support

Introduce new TSM Measurement helper library (tsm-mr) for TVM guest drivers to expose MRs (Measurement Registers) as sysfs attributes, with Crypto Agility support.

Add the following new APIs (see include/linux/tsm-mr.h for details):

- tsm_mr_create_attribute_group(): Take on input a struct tsm_measurements instance, which includes one struct tsm_measurement_register per MR with properties like TSM_MR_F_READABLE and TSM_MR_F_WRITABLE, to determine the supported operations and create the sysfs attributes accordingly. On success, return a struct attribute_group instance that will typically be included by the guest driver into miscdevice.groups before calling misc_register().

- tsm_mr_free_attribute_group(): Free the memory allocated to the attrubute group returned by tsm_mr_create_attribute_group().

tsm_mr_create_attribute_group() creates one attribute for each MR, with names following this pattern:

MRNAME[:HASH]

- MRNAME - Placeholder for the MR name, as specified by tsm_measurement_register.mr_name.
- :HASH - Optional suffix indicating the hash algorithm associated with this MR, as specified by tsm_measurement_register.mr_hash.

Support Crypto Agility by allowing multiple definitions of the same MR (i.e., with the same mr_name) with distinct HASH algorithms.

NOTE: Crypto Agility, introduced in TPM 2.0, allows new hash algorithms to be introduced without breaking compatibility with applications using older algorithms. CC architectures may face the same challenge in the future, needing new hashes for security while retaining compatibility with older hashes, hence the need for Crypto Agility.

---
 Documentation/driver-api/coco/index.rst            |  12 +
 .../driver-api/coco/measurement-registers.rst      |  12 +
 Documentation/driver-api/index.rst                 |   1 +
 MAINTAINERS                                        |   4 +-
 drivers/virt/coco/Kconfig                          |   5 +
 drivers/virt/coco/Makefile                         |   1 +
 drivers/virt/coco/tsm-mr.c                         | 247 +++++++++++++++++++++
 include/linux/tsm-mr.h                             |  94 ++++++++
 include/trace/events/tsm_mr.h                      |  80 +++++++
 9 files changed, 454 insertions(+), 2 deletions(-)
Documentation/driver-api/coco/index.rst new file +12 -0
new file mode 100644
index 000000000000..af9f08ca0cfd
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======================
+Confidential Computing
+======================
+
+.. toctree::
+   :maxdepth: 1
+
+   measurement-registers
+
+.. only::  subproject and html
Documentation/driver-api/coco/measurement-registers.rst new file +12 -0
new file mode 100644
index 000000000000..cef85945a9a7
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. include:: <isonum.txt>
+
+=====================
+Measurement Registers
+=====================
+
+.. kernel-doc:: include/linux/tsm-mr.h
+   :internal:
+
+.. kernel-doc:: drivers/virt/coco/tsm-mr.c
+   :export:
Documentation/driver-api/index.rst +1 -0
index 16e2c4ec3c01..3e2a270bd828 100644
@@ -81,6 +81,7 @@ Subsystem-specific APIs
    acpi/index
    backlight/lp855x-driver.rst
    clk
+   coco/index
    console
    crypto/index
    dmaengine/index
MAINTAINERS +2 -2
index fa1e04e87d1d..1d1426af3a68 100644
@@ -24616,8 +24616,8 @@ M:	Dan Williams <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/ABI/testing/configfs-tsm
-F:	drivers/virt/coco/tsm.c
-F:	include/linux/tsm.h
+F:	drivers/virt/coco/tsm*.c
+F:	include/linux/tsm*.h
 
 TRUSTED SERVICES TEE DRIVER
 M:	Balint Dobszay <[email protected]>
drivers/virt/coco/Kconfig +5 -0
index ff869d883d95..737106d5dcbc 100644
@@ -7,6 +7,11 @@ config TSM_REPORTS
 	select CONFIGFS_FS
 	tristate
 
+config TSM_MEASUREMENTS
+	select CRYPTO_HASH_INFO
+	select CRYPTO
+	bool
+
 source "drivers/virt/coco/efi_secret/Kconfig"
 
 source "drivers/virt/coco/pkvm-guest/Kconfig"
drivers/virt/coco/Makefile +1 -0
index c3d07cfc087e..eb6ec5c1d2e1 100644
@@ -3,6 +3,7 @@
 # Confidential computing related collateral
 #
 obj-$(CONFIG_TSM_REPORTS)	+= tsm.o
+obj-$(CONFIG_TSM_MEASUREMENTS)	+= tsm-mr.o
 obj-$(CONFIG_EFI_SECRET)	+= efi_secret/
 obj-$(CONFIG_ARM_PKVM_GUEST)	+= pkvm-guest/
 obj-$(CONFIG_SEV_GUEST)		+= sev-guest/
drivers/virt/coco/tsm-mr.c new file +247 -0

247 lines not on this page · raw message

include/linux/tsm-mr.h new file +94 -0

94 lines not on this page · raw message

include/trace/events/tsm_mr.h new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v5 1/5
#2 lore raw

Re: [PATCH v5 1/5] tsm-mr: Add TVM Measurement Register support

Quoted 30 lines · Cedric Xing

Introduce new TSM Measurement helper library (tsm-mr) for TVM guest drivers to expose MRs (Measurement Registers) as sysfs attributes, with Crypto Agility support.

Add the following new APIs (see include/linux/tsm-mr.h for details):

- tsm_mr_create_attribute_group(): Take on input a struct tsm_measurements instance, which includes one struct tsm_measurement_register per MR with properties like TSM_MR_F_READABLE and TSM_MR_F_WRITABLE, to determine the supported operations and create the sysfs attributes accordingly. On success, return a struct attribute_group instance that will typically be included by the guest driver into miscdevice.groups before calling misc_register().

- tsm_mr_free_attribute_group(): Free the memory allocated to the attrubute group returned by tsm_mr_create_attribute_group().

tsm_mr_create_attribute_group() creates one attribute for each MR, with names following this pattern:

MRNAME[:HASH]

- MRNAME - Placeholder for the MR name, as specified by tsm_measurement_register.mr_name.
- :HASH - Optional suffix indicating the hash algorithm associated with this MR, as specified by tsm_measurement_register.mr_hash.

Support Crypto Agility by allowing multiple definitions of the same MR (i.e., with the same mr_name) with distinct HASH algorithms.

Quoted 7 lines

NOTE: Crypto Agility, introduced in TPM 2.0, allows new hash algorithms to be introduced without breaking compatibility with applications using older algorithms. CC architectures may face the same challenge in the future, needing new hashes for security while retaining compatibility with older hashes, hence the need for Crypto Agility.

Looking good, some minor fixups below.

Quoted 65 lines
---
 Documentation/driver-api/coco/index.rst            |  12 +
 .../driver-api/coco/measurement-registers.rst      |  12 +
 Documentation/driver-api/index.rst                 |   1 +
 MAINTAINERS                                        |   4 +-
 drivers/virt/coco/Kconfig                          |   5 +
 drivers/virt/coco/Makefile                         |   1 +
 drivers/virt/coco/tsm-mr.c                         | 247 +++++++++++++++++++++
 include/linux/tsm-mr.h                             |  94 ++++++++
 include/trace/events/tsm_mr.h                      |  80 +++++++
 9 files changed, 454 insertions(+), 2 deletions(-)
Documentation/driver-api/coco/index.rst new file +12 -0
new file mode 100644
index 000000000000..af9f08ca0cfd
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======================
+Confidential Computing
+======================
+
+.. toctree::
+   :maxdepth: 1
+
+   measurement-registers
+
+.. only::  subproject and html
Documentation/driver-api/coco/measurement-registers.rst new file +12 -0
new file mode 100644
index 000000000000..cef85945a9a7
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. include:: <isonum.txt>
+
+=====================
+Measurement Registers
+=====================
+
+.. kernel-doc:: include/linux/tsm-mr.h
+   :internal:
+
+.. kernel-doc:: drivers/virt/coco/tsm-mr.c
+   :export:
Documentation/driver-api/index.rst +1 -0
index 16e2c4ec3c01..3e2a270bd828 100644
@@ -81,6 +81,7 @@ Subsystem-specific APIs
    acpi/index
    backlight/lp855x-driver.rst
    clk
+   coco/index
    console
    crypto/index
    dmaengine/index
MAINTAINERS +0 -0
index fa1e04e87d1d..1d1426af3a68 100644

MAINTAINERS M: Dan Williams <[email protected]> · line 24620

Quoted 7 lines
MAINTAINERS +2 -2
@@ -24616,8 +24616,8 @@ M:	Dan Williams <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/ABI/testing/configfs-tsm
-F:	drivers/virt/coco/tsm.c
-F:	include/linux/tsm.h
+F:	drivers/virt/coco/tsm*.c
+F:	include/linux/tsm*.h

Should also add Documentation/driver-api/coco/ here.

Quoted 132 lines
MAINTAINERS +0 -0
 TRUSTED SERVICES TEE DRIVER
 M:	Balint Dobszay <[email protected]>
drivers/virt/coco/Kconfig +5 -0
index ff869d883d95..737106d5dcbc 100644
@@ -7,6 +7,11 @@ config TSM_REPORTS
 	select CONFIGFS_FS
 	tristate
 
+config TSM_MEASUREMENTS
+	select CRYPTO_HASH_INFO
+	select CRYPTO
+	bool
+
 source "drivers/virt/coco/efi_secret/Kconfig"
 
 source "drivers/virt/coco/pkvm-guest/Kconfig"
drivers/virt/coco/Makefile +1 -0
index c3d07cfc087e..eb6ec5c1d2e1 100644
@@ -3,6 +3,7 @@
 # Confidential computing related collateral
 #
 obj-$(CONFIG_TSM_REPORTS)	+= tsm.o
+obj-$(CONFIG_TSM_MEASUREMENTS)	+= tsm-mr.o
 obj-$(CONFIG_EFI_SECRET)	+= efi_secret/
 obj-$(CONFIG_ARM_PKVM_GUEST)	+= pkvm-guest/
 obj-$(CONFIG_SEV_GUEST)		+= sev-guest/
drivers/virt/coco/tsm-mr.c new file +95 -0
new file mode 100644
index 000000000000..adc32328018e
@@ -0,0 +1,247 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/tsm_mr.h>
+
+/*
+ * struct tm_context - contains everything necessary to implement sysfs
+ * attributes for MRs.
+ * @rwsem: protects the MR cache from concurrent access.
+ * @agrp: contains all MR attributes created by tsm_mr_create_attribute_group().
+ * @tm: input to tsm_mr_create_attribute_group() containing MR definitions/ops.
+ * @in_sync: %true if MR cache is up-to-date.
+ * @mrs: array of &struct bin_attribute, one for each MR.
+ *
+ * This internal structure contains everything needed to implement
+ * tm_digest_read() and tm_digest_write().
+ *
+ * Given tm->refresh() is potentially expensive, tm_digest_read() caches MR
+ * values and calls tm->refresh() only when necessary. Only live MRs (i.e., with
+ * %TSM_MR_F_LIVE set) can trigger tm->refresh(), while others are assumed to
+ * retain their values from the last tm->write(). @in_sync tracks if there have
+ * been tm->write() calls since the last tm->refresh(). That is, tm->refresh()
+ * will be called only when a live MR is being read and the cache is stale
+ * (@in_sync is %false).
+ *
+ * tm_digest_write() sets @in_sync to %false and calls tm->write(), whose
+ * semantics is arch and MR specific. Most (if not all) writable MRs support the
+ * extension semantics (i.e., tm->write() extends the input buffer into the MR).
+ */
+struct tm_context {
+	struct rw_semaphore rwsem;
+	struct attribute_group agrp;
+	const struct tsm_measurements *tm;
+	bool in_sync;
+	struct bin_attribute mrs[];
+};
+
+static ssize_t tm_digest_read(struct file *filp, struct kobject *kobj,
+			      const struct bin_attribute *attr, char *buffer,
+			      loff_t off, size_t count)
+{
+	struct tm_context *ctx;
+	const struct tsm_measurement_register *mr;
+	int rc;
+
+	ctx = attr->private;
+	rc = down_read_interruptible(&ctx->rwsem);
+	if (rc)
+		return rc;
+
+	mr = &ctx->tm->mrs[attr - ctx->mrs];
+
+	/*
+	 * @ctx->in_sync indicates if the MR cache is stale. It is a global
+	 * instead of a per-MR flag for simplicity, as most (if not all) archs
+	 * allow reading all MRs in oneshot.
+	 *
+	 * ctx->refresh() is necessary only for LIVE MRs, while others retain
+	 * their values from their respective last ctx->write().
+	 */
+	if ((mr->mr_flags & TSM_MR_F_LIVE) && !ctx->in_sync) {
+		up_read(&ctx->rwsem);
+
+		rc = down_write_killable(&ctx->rwsem);
+		if (rc)
+			return rc;
+
+		if (!ctx->in_sync) {
+			rc = ctx->tm->refresh(ctx->tm, mr);
+			ctx->in_sync = !rc;
+			trace_tsm_mr_refresh(mr, rc);
+		}
+
+		downgrade_write(&ctx->rwsem);
+	}
+
+	memcpy(buffer, mr->mr_value + off, count);
+	trace_tsm_mr_read(mr);
+
+	up_read(&ctx->rwsem);
+	return rc ?: count;
+}
+
+static ssize_t tm_digest_write(struct file *filp, struct kobject *kobj,
+			       const struct bin_attribute *attr, char *buffer,
+			       loff_t off, size_t count)
+{
+	struct tm_context *ctx;

drivers/virt/coco/tsm-mr.c line 105

Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10 -0
+	const struct tsm_measurement_register *mr;
+	ssize_t rc;
+
+	/* partial writes are not supported */
+	if (off != 0 || count != attr->size)
+		return -EINVAL;
+
+	ctx = attr->private;
+	mr = &ctx->tm->mrs[attr - ctx->mrs];
+	trace_tsm_mr_write(mr, buffer);

I would have expected the trace to be emitted after the write succeeds, otherwise this tracepoint is "may have written" vs "write".

drivers/virt/coco/tsm-mr.c line 115

Quoted 10 lines
drivers/virt/coco/tsm-mr.c new file +10 -0
+
+	rc = down_write_killable(&ctx->rwsem);
+	if (rc)
+		return rc;
+
+	rc = ctx->tm->write(ctx->tm, mr, buffer);
+
+	/* mark MR cache stale */
+	if (!rc)
+		ctx->in_sync = false;

...also, putting the trace here means the trace event is known to be ordered by the rwsem.

[..]

Other than the above you can add:

#3 lore raw

PATCH v5 2/5 tsm-mr: Add tsm-mr sample code

In reply to Cedric Xing

This sample kernel module demonstrates how to make MRs accessible to user mode through the tsm-mr library.

Once loaded, this module registers a miscdevice that host a set of emulated measurement registers as shown in the directory tree below.

/sys/class/misc/tsm_mr_sample

└── mr
    ├── config_mr
    ├── report_digest:sha512
    ├── rtmr0:sha256
    ├── rtmr1:sha384
    ├── rtmr_crypto_agile:sha256
    ├── rtmr_crypto_agile:sha384
    └── static_mr:sha384

Among the MRs in this example:

- config_mr demonstrates a hashless MR, like MRCONFIGID in Intel TDX or HOSTDATA in AMD SEV.
- static_mr demonstrates a static MR. The suffix :sha384 indicates its value is a sha384 digest.
- rtmr0 is an RTMR with TSM_MR_F_WRITABLE **cleared**, preventing direct extensions; as a result, the attribute rtmr0:sha256 is read-only.
- rtmr1 is an RTMR with TSM_MR_F_WRITABLE **set**, permitting direct extensions; thus, the attribute rtmr1:sha384 is writable.
- rtmr_crypto_agile demonstrates a "single" MR that supports multiple hash algorithms. Each supported algorithm has a corresponding digest, usually referred to as a "bank" in TCG terminology. In this specific sample, the 2 banks are aliased to rtmr0 and rtmr1, respectively.
- report_digest contains the digest of the internal report structure living in this sample module's memory. It is to demonstrate the use of the TSM_MR_F_LIVE flag. Its value changes each time an RTMR is extended.

---
 MAINTAINERS                    |   1 +
 samples/Kconfig                |  11 ++++
 samples/Makefile               |   1 +
 samples/tsm-mr/Makefile        |   2 +
 samples/tsm-mr/tsm_mr_sample.c | 137 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 152 insertions(+)
MAINTAINERS +1 -0
index 1d1426af3a68..fd5bbf78ec8b 100644
@@ -24618,6 +24618,7 @@ S:	Maintained
 F:	Documentation/ABI/testing/configfs-tsm
 F:	drivers/virt/coco/tsm*.c
 F:	include/linux/tsm*.h
+F:	samples/tsm/*
 
 TRUSTED SERVICES TEE DRIVER
 M:	Balint Dobszay <[email protected]>
samples/Kconfig +11 -0
index 09011be2391a..6ade17cb16b4 100644
@@ -184,6 +184,17 @@ config SAMPLE_TIMER
 	bool "Timer sample"
 	depends on CC_CAN_LINK && HEADERS_INSTALL
 
+config SAMPLE_TSM_MR
+	tristate "TSM measurement sample"
+	select TSM_MEASUREMENTS
+	select VIRT_DRIVERS
+	help
+	  Build a sample module that emulates MRs (Measurement Registers) and
+	  exposes them to user mode applications through the TSM sysfs
+	  interface (/sys/class/misc/tsm_mr_sample/emulated_mr/).
+
+	  The module name will be tsm-mr-sample when built as a module.
+
 config SAMPLE_UHID
 	bool "UHID sample"
 	depends on CC_CAN_LINK && HEADERS_INSTALL
samples/Makefile +1 -0
index bf6e6fca5410..c95bac31851c 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_SAMPLES_RUST)		+= rust/
 obj-$(CONFIG_SAMPLE_DAMON_WSSE)		+= damon/
 obj-$(CONFIG_SAMPLE_DAMON_PRCL)		+= damon/
 obj-$(CONFIG_SAMPLE_HUNG_TASK)		+= hung_task/
+obj-$(CONFIG_SAMPLE_TSM_MR)		+= tsm-mr/
samples/tsm-mr/Makefile new file +2 -0
new file mode 100644
index 000000000000..587c3947b3a7
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_SAMPLE_TSM_MR) += tsm_mr_sample.o
samples/tsm-mr/tsm_mr_sample.c new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v5 2/5
#4 lore raw

Re: [PATCH v5 2/5] tsm-mr: Add tsm-mr sample code

Quoted 102 lines · Cedric Xing

This sample kernel module demonstrates how to make MRs accessible to user mode through the tsm-mr library.

Once loaded, this module registers a miscdevice that host a set of emulated measurement registers as shown in the directory tree below.

/sys/class/misc/tsm_mr_sample

└── mr
    ├── config_mr
    ├── report_digest:sha512
    ├── rtmr0:sha256
    ├── rtmr1:sha384
    ├── rtmr_crypto_agile:sha256
    ├── rtmr_crypto_agile:sha384
    └── static_mr:sha384

Among the MRs in this example:

- config_mr demonstrates a hashless MR, like MRCONFIGID in Intel TDX or HOSTDATA in AMD SEV.
- static_mr demonstrates a static MR. The suffix :sha384 indicates its value is a sha384 digest.
- rtmr0 is an RTMR with TSM_MR_F_WRITABLE **cleared**, preventing direct extensions; as a result, the attribute rtmr0:sha256 is read-only.
- rtmr1 is an RTMR with TSM_MR_F_WRITABLE **set**, permitting direct extensions; thus, the attribute rtmr1:sha384 is writable.
- rtmr_crypto_agile demonstrates a "single" MR that supports multiple hash algorithms. Each supported algorithm has a corresponding digest, usually referred to as a "bank" in TCG terminology. In this specific sample, the 2 banks are aliased to rtmr0 and rtmr1, respectively.
- report_digest contains the digest of the internal report structure living in this sample module's memory. It is to demonstrate the use of the TSM_MR_F_LIVE flag. Its value changes each time an RTMR is extended.

---
 MAINTAINERS                    |   1 +
 samples/Kconfig                |  11 ++++
 samples/Makefile               |   1 +
 samples/tsm-mr/Makefile        |   2 +
 samples/tsm-mr/tsm_mr_sample.c | 137 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 152 insertions(+)
MAINTAINERS +1 -0
index 1d1426af3a68..fd5bbf78ec8b 100644
@@ -24618,6 +24618,7 @@ S:	Maintained
 F:	Documentation/ABI/testing/configfs-tsm
 F:	drivers/virt/coco/tsm*.c
 F:	include/linux/tsm*.h
+F:	samples/tsm/*
 
 TRUSTED SERVICES TEE DRIVER
 M:	Balint Dobszay <[email protected]>
samples/Kconfig +11 -0
index 09011be2391a..6ade17cb16b4 100644
@@ -184,6 +184,17 @@ config SAMPLE_TIMER
 	bool "Timer sample"
 	depends on CC_CAN_LINK && HEADERS_INSTALL
 
+config SAMPLE_TSM_MR
+	tristate "TSM measurement sample"
+	select TSM_MEASUREMENTS
+	select VIRT_DRIVERS
+	help
+	  Build a sample module that emulates MRs (Measurement Registers) and
+	  exposes them to user mode applications through the TSM sysfs
+	  interface (/sys/class/misc/tsm_mr_sample/emulated_mr/).
+
+	  The module name will be tsm-mr-sample when built as a module.
+
 config SAMPLE_UHID
 	bool "UHID sample"
 	depends on CC_CAN_LINK && HEADERS_INSTALL
samples/Makefile +1 -0
index bf6e6fca5410..c95bac31851c 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_SAMPLES_RUST)		+= rust/
 obj-$(CONFIG_SAMPLE_DAMON_WSSE)		+= damon/
 obj-$(CONFIG_SAMPLE_DAMON_PRCL)		+= damon/
 obj-$(CONFIG_SAMPLE_HUNG_TASK)		+= hung_task/
+obj-$(CONFIG_SAMPLE_TSM_MR)		+= tsm-mr/
samples/tsm-mr/Makefile new file +2 -0
new file mode 100644
index 000000000000..587c3947b3a7
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_SAMPLE_TSM_MR) += tsm_mr_sample.o
samples/tsm-mr/tsm_mr_sample.c new file +0 -0
new file mode 100644
index 000000000000..3ee35d87d275

samples/tsm-mr/tsm_mr_sample.c line 2

Quoted 2 lines
samples/tsm-mr/tsm_mr_sample.c new file +2 -0
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024 Intel Corporation. All rights reserved. */

2024-2025

samples/tsm-mr/tsm_mr_sample.c line 4

Quoted 2 lines
samples/tsm-mr/tsm_mr_sample.c new file +2 -0
+
+#define DEBUG

No need for this. Either the person trying out this sample driver will define DEBUG at compile time, manually add this line, or otherwise use dynamic-debug to enable the pr_debug() statements when loading the module.

If you put #define DEBUG directly in the file then pr_debug() just becomes printk() which defeats the purpose.

Quoted 17 lines
samples/tsm-mr/tsm_mr_sample.c new file +17 -0
+#define pr_fmt(x) KBUILD_MODNAME ": " x
+
+#include <linux/module.h>
+#include <linux/tsm-mr.h>
+#include <linux/miscdevice.h>
+#include <crypto/hash.h>
+
+struct {
+	u8 static_mr[SHA384_DIGEST_SIZE];
+	u8 config_mr[SHA512_DIGEST_SIZE];
+	u8 rtmr0[SHA256_DIGEST_SIZE];
+	u8 rtmr1[SHA384_DIGEST_SIZE];
+	u8 report_digest[SHA512_DIGEST_SIZE];
+} sample_report = {
+	.static_mr = "static_mr",
+	.config_mr = "config_mr",
+	.rtmr0 = "rtmr0",

samples/tsm-mr/tsm_mr_sample.c line 31

Quoted 10 lines
samples/tsm-mr/tsm_mr_sample.c new file +10 -0
+	.rtmr1 = "rtmr1",
+};
+
+static int sample_report_refresh(const struct tsm_measurements *tm,
+				 const struct tsm_measurement_register *mr)
+{
+	struct crypto_shash *tfm;
+	int rc;
+
+	pr_debug("%s(%s) is called\n", __func__, mr ? mr->mr_name : "<nil>");

With dynamic-debug the function name can be enabled, so no need to include that in the debug statement.

See "Decorator flags add to the message-prefix" in Documentation/admin-guide/dynamic-debug-howto.rst.

Other than that this looks good to me:

#5 lore raw

PATCH v5 3/5 x86/tdx: Add tdx_mcall_extend_rtmr() interface

In reply to Cedric Xing

The TDX guest exposes one MRTD (Build-time Measurement Register) and four RTMR (Run-time Measurement Register) registers to record the build and boot measurements of a virtual machine (VM). These registers are similar to PCR (Platform Configuration Register) registers in the TPM (Trusted Platform Module) space. This measurement data is used to implement security features like attestation and trusted boot.

To facilitate updating the RTMR registers, the TDX module provides support for the TDG.MR.RTMR.EXTEND TDCALL which can be used to securely extend the RTMR registers.

Add helper function to update RTMR registers. It will be used by the TDX guest driver in enabling RTMR extension support.

---
 arch/x86/coco/tdx/tdx.c           | 37 +++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/shared/tdx.h |  1 +
 arch/x86/include/asm/tdx.h        |  2 ++
 3 files changed, 40 insertions(+)
arch/x86/coco/tdx/tdx.c +37 -0
index edab6d6049be..0b6804d2a5e1 100644
@@ -36,6 +36,7 @@
 /* TDX Module call error codes */
 #define TDCALL_RETURN_CODE(a)	((a) >> 32)
 #define TDCALL_INVALID_OPERAND	0xc0000100
+#define TDCALL_OPERAND_BUSY	0x80000200
 
 #define TDREPORT_SUBTYPE_0	0
 
@@ -136,6 +137,42 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
 }
 EXPORT_SYMBOL_GPL(tdx_mcall_get_report0);
 
+/**
+ * tdx_mcall_extend_rtmr() - Wrapper to extend RTMR registers using
+ *			     TDG.MR.RTMR.EXTEND TDCALL.
+ * @index: Index of RTMR register to be extended.
+ * @data: Address of the input buffer with RTMR register extend data.
+ *
+ * Refer to section titled "TDG.MR.RTMR.EXTEND leaf" in the TDX Module v1.0
+ * specification for more information on TDG.MR.RTMR.EXTEND TDCALL.
+ *
+ * It is used in the TDX guest driver module to allow user to extend the RTMR
+ * registers.
+ *
+ * Return 0 on success, -ENXIO for invalid operands, -EBUSY for busy operation,
+ * or -EIO on other TDCALL failures.
+ */
+int tdx_mcall_extend_rtmr(u8 index, u8 *data)
+{
+	struct tdx_module_args args = {
+		.rcx = virt_to_phys(data),
+		.rdx = index,
+	};
+	u64 ret;
+
+	ret = __tdcall(TDG_MR_RTMR_EXTEND, &args);
+	if (ret) {
+		if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+			return -ENXIO;
+		if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
+			return -EBUSY;
+		return -EIO;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(tdx_mcall_extend_rtmr);
+
 /**
  * tdx_hcall_get_quote() - Wrapper to request TD Quote using GetQuote
  *                         hypercall.
arch/x86/include/asm/shared/tdx.h +1 -0
index a28ff6b14145..738f583f65cb 100644
@@ -13,6 +13,7 @@
 /* TDX module Call Leaf IDs */
 #define TDG_VP_VMCALL			0
 #define TDG_VP_INFO			1
+#define TDG_MR_RTMR_EXTEND		2
 #define TDG_VP_VEINFO_GET		3
 #define TDG_MR_REPORT			4
 #define TDG_MEM_PAGE_ACCEPT		6
arch/x86/include/asm/tdx.h +2 -0
index 4a1922ec80cf..12d17f3ca301 100644
@@ -64,6 +64,8 @@ bool tdx_early_handle_ve(struct pt_regs *regs);
 
 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport);
 
+int tdx_mcall_extend_rtmr(u8 index, u8 *data);
+
 u64 tdx_hcall_get_quote(u8 *buf, size_t size);
 
 void __init tdx_dump_attributes(u64 td_attr);
Signature · 1 line
2.43.0
Acked-by on v5 3/5
#6 lore raw

Re: [PATCH v5 3/5] x86/tdx: Add tdx_mcall_extend_rtmr() interface

Quoted 7 lines · Cedric Xing

The TDX guest exposes one MRTD (Build-time Measurement Register) and four RTMR (Run-time Measurement Register) registers to record the build and boot measurements of a virtual machine (VM). These registers are similar to PCR (Platform Configuration Register) registers in the TPM (Trusted Platform Module) space. This measurement data is used to implement security features like attestation and trusted boot.

Quoted 6 lines

To facilitate updating the RTMR registers, the TDX module provides support for the TDG.MR.RTMR.EXTEND TDCALL which can be used to securely extend the RTMR registers.

Add helper function to update RTMR registers. It will be used by the TDX guest driver in enabling RTMR extension support.

Thank you for revising the changelogs to make the underlying purpose of this series more clear.

#7 lore raw

Re: [PATCH v5 3/5] x86/tdx: Add tdx_mcall_extend_rtmr() interface

In reply to Cedric Xing

arch/x86/coco/tdx/tdx.c int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport) · line 170

Quoted 8 lines · Cedric Xing
arch/x86/coco/tdx/tdx.c +8 -0
+	ret = __tdcall(TDG_MR_RTMR_EXTEND, &args);
+	if (ret) {
+		if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+			return -ENXIO;
+		if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
+			return -EBUSY;
+		return -EIO;
+	}

This a pretty ugly switch statement. ;)

I assume there are more of these around, but it would be _nice_ if these could eventually look like:

	...
	err = __tdcall(...);

	return tdx_err_to_errno(err);
#8 lore raw

PATCH v5 4/5 x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error

In reply to Cedric Xing

Return -EBUSY from tdx_mcall_get_report0() when TDG.MR.REPORT returns TDCALL_OPERAND_BUSY. This enables the caller to retry obtaining a TDREPORT later if another VCPU is extending an RTMR concurrently.

---
 arch/x86/coco/tdx/tdx.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/coco/tdx/tdx.c +8 -5
index 0b6804d2a5e1..7b2833705d47 100644
@@ -110,12 +110,13 @@ static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
  *              REPORTDATA to be included into TDREPORT.
  * @tdreport: Address of the output buffer to store TDREPORT.
  *
- * Refer to section titled "TDG.MR.REPORT leaf" in the TDX Module
- * v1.0 specification for more information on TDG.MR.REPORT TDCALL.
+ * Refer to section titled "TDG.MR.REPORT leaf" in the TDX Module v1.0
+ * specification for more information on TDG.MR.REPORT TDCALL.
+ *
  * It is used in the TDX guest driver module to get the TDREPORT0.
  *
- * Return 0 on success, -EINVAL for invalid operands, or -EIO on
- * other TDCALL failures.
+ * Return 0 on success, -ENXIO for invalid operands, -EBUSY for busy operation,
+ * or -EIO on other TDCALL failures.
  */
 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
 {
@@ -129,7 +130,9 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
 	ret = __tdcall(TDG_MR_REPORT, &args);
 	if (ret) {
 		if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
-			return -EINVAL;
+			return -ENXIO;
+		else if (TDCALL_RETURN_CODE(ret) == TDCALL_OPERAND_BUSY)
+			return -EBUSY;
 		return -EIO;
 	}
 
Signature · 1 line
2.43.0
Acked-by on v5 4/5
#9 lore raw

Re: [PATCH v5 4/5] x86/tdx: tdx_mcall_get_report0: Return -EBUSY on TDCALL_OPERAND_BUSY error

Quoted 3 lines · Cedric Xing

Return -EBUSY from tdx_mcall_get_report0() when TDG.MR.REPORT returns TDCALL_OPERAND_BUSY. This enables the caller to retry obtaining a TDREPORT later if another VCPU is extending an RTMR concurrently.

I would appreciate if someone can go back and look at centralizing the TDX error code => errno conversions. But that doesn't need to happen now.

#10 lore raw

PATCH v5 5/5 virt: tdx-guest: Expose TDX MRs as sysfs attributes

In reply to Cedric Xing

Expose the most commonly used TDX MRs (Measurement Registers) as sysfs attributes. Use the ioctl() interface of /dev/tdx_guest to request a full TDREPORT for access to other TD measurements.

Directory structure of TDX MRs inside a TDVM is as follows:

/sys/class/misc/tdx_guest

└── mr
    ├── mrconfigid
    ├── mrowner
    ├── mrownerconfig
    ├── mrtd:sha384
    ├── rtmr0:sha384
    ├── rtmr1:sha384
    ├── rtmr2:sha384
    └── rtmr3:sha384

Read the file/attribute to retrieve the current value of an MR. Write to the file/attribute (if writable) to extend the corresponding RTMR. Refer to Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest for more information.

---
 .../testing/sysfs-devices-virtual-misc-tdx_guest   |  60 +++++
 MAINTAINERS                                        |   1 +
 drivers/virt/coco/tdx-guest/Kconfig                |   1 +
 drivers/virt/coco/tdx-guest/tdx-guest.c            | 291 +++++++++++++--------
 4 files changed, 250 insertions(+), 103 deletions(-)
Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest new file +60 -0
new file mode 100644
index 000000000000..fb7b27ae0b74
@@ -0,0 +1,60 @@
+What:		/sys/devices/virtual/misc/tdx_guest/mr/MRNAME[:HASH]
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		Value of a TDX measurement register (MR). MRNAME and HASH above
+		are placeholders. The optional suffix :HASH is used for MRs
+		that have associated hash algorithms. See below for a complete
+		list of TDX MRs exposed via sysfs. Refer to Intel TDX Module
+		ABI Specification for the definition of TDREPORT and the full
+		list of TDX measurements.
+
+		Intel TDX Module ABI Specification can be found at:
+		https://www.intel.com/content/www/us/en/developer/tools/trust-domain-extensions/documentation.html#architecture
+
+What:		/sys/devices/virtual/misc/tdx_guest/mr/mrconfigid
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		(RO) MRCONFIGID - 48-byte immutable storage typically used for
+		software-defined ID for non-owner-defined configuration of the
+		guest TD – e.g., run-time or OS configuration.
+
+What:		/sys/devices/virtual/misc/tdx_guest/mr/mrowner
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		(RO) MROWNER - 48-byte immutable storage typically used for
+		software-defined ID for the guest TD’s owner.
+
+What:		/sys/devices/virtual/misc/tdx_guest/mr/mrownerconfig
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		(RO) MROWNERCONFIG - 48-byte immutable storage typically used
+		for software-defined ID for owner-defined configuration of the
+		guest TD – e.g., specific to the workload rather than the
+		run-time or OS.
+
+What:		/sys/devices/virtual/misc/tdx_guest/mr/mrtd:sha384
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		(RO) MRTD - Measurement of the initial contents of the TD.
+
+What:		/sys/devices/virtual/misc/tdx_guest/mr/rtmr[0123]:sha384
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		(RW) RTMR[0123] - 4 Run-Time extendable Measurement Registers.
+		Read from any of these returns the current value of the
+		corresponding RTMR. Write extends the written buffer to the
+		RTMR. All writes must start at offset 0 and be 48 bytes in
+		size. Partial writes will result in EINVAL returned by the
+		write() syscall.
MAINTAINERS +1 -0
index fd5bbf78ec8b..3afbf6c310a7 100644
@@ -26284,6 +26284,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/tdx
+F:	Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest
 F:	arch/x86/boot/compressed/tdx*
 F:	arch/x86/coco/tdx/
 F:	arch/x86/include/asm/shared/tdx.h
drivers/virt/coco/tdx-guest/Kconfig +1 -0
index 22dd59e19431..dbbdc14383b1 100644
@@ -2,6 +2,7 @@ config TDX_GUEST_DRIVER
 	tristate "TDX Guest driver"
 	depends on INTEL_TDX_GUEST
 	select TSM_REPORTS
+	select TSM_MEASUREMENTS
 	help
 	  The driver provides userspace interface to communicate with
 	  the TDX module to request the TDX guest details like attestation
drivers/virt/coco/tdx-guest/tdx-guest.c +188 -103

360 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v5 5/5] virt: tdx-guest: Expose TDX MRs as sysfs attributes

Quoted 36 lines · Cedric Xing

Expose the most commonly used TDX MRs (Measurement Registers) as sysfs attributes. Use the ioctl() interface of /dev/tdx_guest to request a full TDREPORT for access to other TD measurements.

Directory structure of TDX MRs inside a TDVM is as follows:

/sys/class/misc/tdx_guest

└── mr
    ├── mrconfigid
    ├── mrowner
    ├── mrownerconfig
    ├── mrtd:sha384
    ├── rtmr0:sha384
    ├── rtmr1:sha384
    ├── rtmr2:sha384
    └── rtmr3:sha384

Read the file/attribute to retrieve the current value of an MR. Write to the file/attribute (if writable) to extend the corresponding RTMR. Refer to Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest for more information.

---
 .../testing/sysfs-devices-virtual-misc-tdx_guest   |  60 +++++
 MAINTAINERS                                        |   1 +
 drivers/virt/coco/tdx-guest/Kconfig                |   1 +
 drivers/virt/coco/tdx-guest/tdx-guest.c            | 291 +++++++++++++--------
 4 files changed, 250 insertions(+), 103 deletions(-)
Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest new file +0 -0
new file mode 100644
index 000000000000..fb7b27ae0b74

Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest line 1

Quoted 1 line
Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest new file +1 -0
@@ -0,0 +1,60 @@
+What:		/sys/devices/virtual/misc/tdx_guest/mr/MRNAME[:HASH]

I missed this on patch1, but I am still not a fan of this attribute directory being called "mr". Please make it "measurement_registers" or "measurements" to remove ambiguity.

Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest line 14

Quoted 13 lines
Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest new file +13 -0
+Date:		April, 2025
+KernelVersion:	v6.16
+Contact:	[email protected]
+Description:
+		Value of a TDX measurement register (MR). MRNAME and HASH above
+		are placeholders. The optional suffix :HASH is used for MRs
+		that have associated hash algorithms. See below for a complete
+		list of TDX MRs exposed via sysfs. Refer to Intel TDX Module
+		ABI Specification for the definition of TDREPORT and the full
+		list of TDX measurements.
+
+		Intel TDX Module ABI Specification can be found at:
+		https://www.intel.com/content/www/us/en/developer/tools/trust-domain-extensions/documentation.html#architecture

Documentation indeed looks better now. I had also been wanting this ABI documentation to refer to a generic description of how measurement-register sysfs files behave. I think you can achieve that simply with a link back to the generated driver-api kdoc:

https://docs.kernel.org/driver-api/coco/measurement-registers.html

Of course, that link will start resolving once this is upstream.

[..]

Quoted 72 lines
MAINTAINERS +1 -0
index fd5bbf78ec8b..3afbf6c310a7 100644
@@ -26284,6 +26284,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/tdx
+F:	Documentation/ABI/testing/sysfs-devices-virtual-misc-tdx_guest
 F:	arch/x86/boot/compressed/tdx*
 F:	arch/x86/coco/tdx/
 F:	arch/x86/include/asm/shared/tdx.h
drivers/virt/coco/tdx-guest/Kconfig +1 -0
index 22dd59e19431..dbbdc14383b1 100644
@@ -2,6 +2,7 @@ config TDX_GUEST_DRIVER
 	tristate "TDX Guest driver"
 	depends on INTEL_TDX_GUEST
 	select TSM_REPORTS
+	select TSM_MEASUREMENTS
 	help
 	  The driver provides userspace interface to communicate with
 	  the TDX module to request the TDX guest details like attestation
drivers/virt/coco/tdx-guest/tdx-guest.c +23 -2
index 224e7dde9cde..e76810d89e0b 100644
@@ -5,6 +5,8 @@
  * Copyright (C) 2022 Intel Corporation
  */
 
+#define pr_fmt(fmt)			KBUILD_MODNAME ": " fmt
+
 #include <linux/kernel.h>
 #include <linux/miscdevice.h>
 #include <linux/mm.h>
@@ -15,8 +17,9 @@
 #include <linux/set_memory.h>
 #include <linux/io.h>
 #include <linux/delay.h>
+#include <linux/sockptr.h>
 #include <linux/tsm.h>
-#include <linux/sizes.h>
+#include <linux/tsm-mr.h>
 
 #include <uapi/linux/tdx-guest.h>
 
@@ -66,39 +69,144 @@ static DEFINE_MUTEX(quote_lock);
  */
 static u32 getquote_timeout = 30;
 
-static long tdx_get_report0(struct tdx_report_req __user *req)
+/* Buffers for TDREPORT and RTMR extension */
+static u8 *tdx_report_buf, *tdx_extend_buf;
+
+/* Lock to serialize TDG.MR.REPORT & TDG.MR.RTMR.EXTEND requests */
+static DEFINE_MUTEX(mr_lock);
+
+/* TDREPORT fields */
+enum {
+	TDREPORT_reportdata = 128,
+	TDREPORT_tee_tcb_info = 256,
+	TDREPORT_tdinfo = TDREPORT_tee_tcb_info + 256,
+	TDREPORT_attributes = TDREPORT_tdinfo,
+	TDREPORT_xfam = TDREPORT_attributes + sizeof(u64),
+	TDREPORT_mrtd = TDREPORT_xfam + sizeof(u64),
+	TDREPORT_mrconfigid = TDREPORT_mrtd + SHA384_DIGEST_SIZE,
+	TDREPORT_mrowner = TDREPORT_mrconfigid + SHA384_DIGEST_SIZE,
+	TDREPORT_mrownerconfig = TDREPORT_mrowner + SHA384_DIGEST_SIZE,
+	TDREPORT_rtmr0 = TDREPORT_mrownerconfig + SHA384_DIGEST_SIZE,
+	TDREPORT_rtmr1 = TDREPORT_rtmr0 + SHA384_DIGEST_SIZE,

drivers/virt/coco/tdx-guest/tdx-guest.c static DEFINE_MUTEX(quote_lock); · line 98

Quoted 10 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +7 -2
+	TDREPORT_rtmr2 = TDREPORT_rtmr1 + SHA384_DIGEST_SIZE,
+	TDREPORT_rtmr3 = TDREPORT_rtmr2 + SHA384_DIGEST_SIZE,
+	TDREPORT_servtd_hash = TDREPORT_rtmr3 + SHA384_DIGEST_SIZE,
+};
+
+static inline int tdx_mr_report_locked(sockptr_t data, sockptr_t tdreport)
 {
-	u8 *reportdata, *tdreport;
-	long ret;
+	scoped_cond_guard(mutex_intr, return -EINTR, &mr_lock) {

Why a new @mr_lock when @quote_lock exists? Are they not protecting the same things?

Quoted 31 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +17 -10
+		u8 *p = tdx_report_buf + TDREPORT_reportdata;
+		int ret;
 
-	reportdata = kmalloc(TDX_REPORTDATA_LEN, GFP_KERNEL);
-	if (!reportdata)
-		return -ENOMEM;
+		if (!sockptr_is_null(data) &&
+		    copy_from_sockptr(p, data, TDX_REPORTDATA_LEN))
+			return -EFAULT;
 
-	tdreport = kzalloc(TDX_REPORT_LEN, GFP_KERNEL);
-	if (!tdreport) {
-		ret = -ENOMEM;
-		goto out;
+		ret = tdx_mcall_get_report0(p, tdx_report_buf);
+		if (WARN(ret, "tdx_mcall_get_report0() failed: %d", ret))
+			return ret;
+
+		if (!sockptr_is_null(tdreport) &&
+		    copy_to_sockptr(tdreport, tdx_report_buf, TDX_REPORT_LEN))
+			return -EFAULT;
 	}
+	return 0;
+}
 
-	if (copy_from_user(reportdata, req->reportdata, TDX_REPORTDATA_LEN)) {
-		ret = -EFAULT;
-		goto out;
+static inline int tdx_mr_extend_locked(ptrdiff_t mr_ind, const u8 *data)
+{
+	scoped_cond_guard(mutex_intr, return -EINTR, &mr_lock) {

drivers/virt/coco/tdx-guest/tdx-guest.c static DEFINE_MUTEX(quote_lock); · line 129

Quoted 10 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +9 -0
+		int ret;
+
+		memcpy(tdx_extend_buf, data, SHA384_DIGEST_SIZE);
+
+		ret = tdx_mcall_extend_rtmr(mr_ind, tdx_extend_buf);
+		if (WARN(ret, "tdx_mcall_extend_rtmr(%ld) failed: %d", mr_ind, ret))
+			return ret;
 	}
+	return 0;
+}

Ok, the above hunk is an example that this patch is doing too much at once and making the conversion unnecessarily difficult to follow.

There are 3 separate topics in this patch that deserve to be split into 3 patches:

1) Convert tdx_guest to cleanup helpers

Now, I do not personally like the fact that conversion to scoped_cond_guard() causes a bunch of code to be re-indented, that is messy. One alternative to scoped_cond_guard() that I have been considering based on feedback Linus gave when that helper was introduced is instead do something like this:

   DEFINE_FREE(mutex_intr_release, struct mutex, if (_T) mutex_unlock(_T))
   static struct mutex *mutex_intr_acquire(struct mutex *lock)
   {
   	if (mutex_lock_interruptible(lock))
   		return lock;
   	return NULL;
   }

...then you can do:

   struct mutex *lock __free(mutex_intr_release) = mutex_intr_acquire(&mr_lock);

Where that allows you to not need to re-indent the whole function.

If the lock has limited scope within a function then just move that bit to a helper function rather than re-indent existing code.

2) Conversion to sockptr. Prepare the report retrieval code to alternatively store into kernel or user buffers.

3) RTMR support on top of that converted / cleaned-up base. This will be much easier to read withtout the re-indentation mess and other unrelated code movement. Just a clean feature addition patch.