Skip to contentSkip to conversation

TDX: Stop auto-generating the global metadata code

RFC PATCH Superseded Chao Gao 5 participants 31 messages Sep 18 – Sep 28

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Chao Gao

author
#0 lore raw

v2 cover TDX: Stop auto-generating the global metadata code

This series cleans up the TDX global metadata code. It has two goals:

1. Replace the generated code with a table-driven metadata reader.

2. Make the existing code easier to read and maintain, and simplify adding new metadata fields.

During the v1 review, Dave raised concerns about signing off on AI-generated code. I have since rewritten the affected patches based on my own understanding of the code.

The main goal of this RFC is to agree on whether the table-driven reader is the right replacement for the generated code. Please raise any concerns or alternative design ideas.

Feedback on the patch organization is also welcome. In v2, each patch converts one metadata reader. This should make the individual changes easier to review, but results in more patches and some repetition in their changelogs. I am not sure whether this is the best organization for the series.

Dave, please feel free to ignore this RFC. Kirill, Rick, and other TDX developers, please take a look.

Changes since v1:
=================
- Reimplemented the code and rewrote the changelogs for patches generated entirely by AI. (Dave)
- Expanded the problem statement with the history of earlier TDX metadata proposals (Rick)
- Dropped the false claim that literal u64 field IDs are unreviewable

    (Dave)

- Rebase onto tip/x86/tdx and convert the metadata added by DPAMT.
- Name field IDs as their readers are converted instead of naming them in a separate patch.
- Convert one metadata reader per patch.
- v1: https://lore.kernel.org/all/[email protected]/

The TDX module reports its capabilities and limits through a set of global metadata fields, each read using a 64-bit field ID via TDH.SYS.RD. The fields are grouped into classes, and the kernel mirrors each class it needs in a sub-structure of struct tdx_sys_info.

Both those structures and the code that fills them were generated by an out-of-tree script from a JSON file.

The script was not the first approach. Kai's first attempt paired each field ID with its destination C member in a table and walked the table in a loop to read every field. Two pieces of feedback on it drove everything that followed [1]:

1. Compile-time type checking. Metadata fields have different sizes (u16 and u64), so a common helper takes a void * and a size instead of a typed destination.

2. The check that a field ID's encoded size matches its destination member ran at runtime, although both sizes are known at build time.

The discussion did not converge after several rounds of review. Dave noted that, despite the void *, the size check provides the safety that matters:
it catches mismatched field and member widths [2]. That left one problem:
moving the size check from runtime to build time.

Before that was settled, the direction shifted to generating the code with a script. At the time, the TDX ABI definitions were published as JSON, so checking a field ID required consulting a machine-readable file by hand. The script parsed the JSON and generated both the structures and their readers [3]. Generation also made the size/type check unnecessary. The field IDs and destination members came from the same input, so a mismatch could only result from a bug in the script, which is less likely than a mistake in hand-written code.

Dave concluded that the JSON experiment had failed [4] for two reasons:

1. The JSON file is not stable. The CPUID config arrays here were once sized for a maximum of 32 entries, which has since increased to 128 [5].

2. The JSON file is not authoritative enough to write code from by itself.

TDX ABI definitions are now available in human-readable PDF specifications [6]. So, stop relying on the out-of-tree script and maintain the code by hand.

Yilun later proposed a single table whose entries carry the offset and size of each mapped member in struct tdx_sys_info [7]. That design does not cover the new handoff metadata because it is not cached in struct tdx_sys_info.

This series gives every class its own table: each entry pairs a field ID with the member that holds it, and a loop walks the table. This also covers handoff metadata, which is read into a local structure rather than into struct tdx_sys_info.

The common reader still takes a void *, but each mapping verifies at build time that the destination member size matches the size encoded in the field ID. A field/member width mismatch therefore fails the build.

AI usage
========

I used LLM tools to review the patches and refine the wording of the cover letter and changelogs from my drafts. I reviewed all suggestions and adopted those I agreed with. For example, AI review suggested the read_sys_metadata_table() macro, which avoids repeating the table name when passing both the table and its size.

Testing
=======
Built each patch individually and successfully launched TDs.

[1]: https://lore.kernel.org/kvm/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/
[3]: https://lore.kernel.org/kvm/[email protected]/
[4]: https://lore.kernel.org/kvm/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://www.intel.com/content/www/us/en/content-details/865803/abi-definitions-for-intel-tdx.html
[7]: https://lore.kernel.org/kvm/[email protected]/

Chao Gao (10):
x86/virt/tdx: Add a helper to read a table of metadata fields x86/virt/tdx: Convert the version metadata reader x86/virt/tdx: Convert the features metadata reader x86/virt/tdx: Convert the tdmr metadata reader x86/virt/tdx: Convert the td_ctrl metadata reader x86/virt/tdx: Convert the handoff metadata reader x86/virt/tdx: Convert the td_conf metadata reader x86/virt/tdx: Remove tdx_global_metadata.c
x86/virt/tdx: Use early returns in get_tdx_sys_info() x86/virt/tdx: Verify structure member sizes against metadata field IDs

 arch/x86/virt/vmx/tdx/tdx.c                 | 222 +++++++++++++++++++-
 arch/x86/virt/vmx/tdx/tdx.h                 |  54 +++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 154 --------------
 3 files changed, 275 insertions(+), 155 deletions(-)
 delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c
Signature · 1 line
2.52.0
author
#1 lore raw

RFC PATCH v2 1/10 x86/virt/tdx: Add a helper to read a table of metadata fields

The metadata field readers get_tdx_sys_info_<class>() in tdx_global_metadata.c were generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.

Take get_tdx_sys_info_version() as an example:

      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
              sysinfo_version->minor_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
              sysinfo_version->major_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
              sysinfo_version->update_version = val;

Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.

Eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.

Add 'struct field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. Add TDX_SYSINFO_MAP() to build such an entry from a field ID name, a structure type and a member name. Add __read_sys_metadata_table() to read every field in a table. Annotate the helper __maybe_unused as there is no caller right now.

Following changes will convert the existing readers to use the new helper.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +36 -0
index 063574ed8625..5cd9c6eb98c2 100644
@@ -395,6 +395,42 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)
 	return 0;
 }
 
+/*
+ * Map a TDX global metadata field to a structure member.
+ * @field_id: The TDX global metadata field ID.
+ * @size: The size of the structure member.
+ * @offset: The member's offset within its containing structure.
+ */
+struct field_mapping {
+	u64 field_id;
+	size_t size;
+	int offset;
+};
+
+/* Read each metadata field listed in @mappings[] into @data. */
+static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
+						    int num_mappings, void *data)
+{
+	int i, ret;
+	u64 val;
+
+	for (i = 0; i < num_mappings; i++) {
+		ret = read_sys_metadata_field(mappings[i].field_id, &val);
+		if (ret)
+			return ret;
+		memcpy((char *)data + mappings[i].offset, &val, mappings[i].size);
+	}
+
+	return 0;
+}
+
+#define TDX_SYSINFO_MAP(_field, _type, _member)			\
+{								\
+	.field_id	= TDX_MD_FIELD_ID_##_field,		\
+	.offset		= offsetof(_type, _member),		\
+	.size		= sizeof_field(_type, _member),		\
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
Signature · 1 line
2.52.0
#2 lore raw

Re: [RFC PATCH v2 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields

Quoted 15 lines · Chao Gao

The metadata field readers get_tdx_sys_info_<class>() in tdx_global_metadata.c were generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.

Take get_tdx_sys_info_version() as an example:

      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
              sysinfo_version->minor_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
              sysinfo_version->major_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
              sysinfo_version->update_version = val;
Quoted 6 lines

Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.

Eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.

"Prepare to eliminate both..."?

Quoted 48 lines

Add 'struct field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. Add TDX_SYSINFO_MAP() to build such an entry from a field ID name, a structure type and a member name. Add __read_sys_metadata_table() to read every field in a table. Annotate the helper __maybe_unused as there is no caller right now.

Following changes will convert the existing readers to use the new helper.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +22 -0
index 063574ed8625..5cd9c6eb98c2 100644
@@ -395,6 +395,42 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)
 	return 0;
 }
 
+/*
+ * Map a TDX global metadata field to a structure member.
+ * @field_id: The TDX global metadata field ID.
+ * @size: The size of the structure member.
+ * @offset: The member's offset within its containing structure.
+ */
+struct field_mapping {
+	u64 field_id;
+	size_t size;
+	int offset;
+};
+
+/* Read each metadata field listed in @mappings[] into @data. */
+static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
+						    int num_mappings, void *data)
+{
+	int i, ret;
+	u64 val;
+
+	for (i = 0; i < num_mappings; i++) {
+		ret = read_sys_metadata_field(mappings[i].field_id, &val);
+		if (ret)

arch/x86/virt/vmx/tdx/tdx.c static int read_sys_metadata_field(u64 field_id, u64 *data) · line 429

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+			return ret;
+		memcpy((char *)data + mappings[i].offset, &val, mappings[i].size);
+	}
+
+	return 0;
+}
+
+#define TDX_SYSINFO_MAP(_field, _type, _member)			\
+{								\
+	.field_id	= TDX_MD_FIELD_ID_##_field,		\

I find it a bit hard to search the code when it constructs the "TDX_MD_FIELD_ID_" part of the define inside the macro. And since they are all named "TDX_MD_FIELD_ID_FOO" it seems a bit verbose and repetitive. Can we have a smaller pattern such that we can use the exact macro name in TDX_SYSINFO_MAP()? Maybe like this:

#define TDX_FIELD_MODULE_HV 0x8900000100000000ULL

static const struct field_mapping handoff_mappings[] = {
TDX_SYSINFO_MAP_HANDOFF(TDX_FIELD_MODULE_HV, module_hv),
};

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
+	.offset		= offsetof(_type, _member),		\
+	.size		= sizeof_field(_type, _member),		\
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
author
#3 lore raw

Re: [RFC PATCH v2 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields

Quoted 16 lines · Rick P
Quoted 15 lines · Chao Gao

The metadata field readers get_tdx_sys_info_<class>() in tdx_global_metadata.c were generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.

Take get_tdx_sys_info_version() as an example:

      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
              sysinfo_version->minor_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
              sysinfo_version->major_version = val;
      if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
              sysinfo_version->update_version = val;
Quoted 8 lines
Quoted 6 lines

Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.

Eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.

"Prepare to eliminate both..."?

Yes, that makes sense since this patch doesn't convert any readers.

Quoted 10 lines

arch/x86/virt/vmx/tdx/tdx.c static int read_sys_metadata_field(u64 field_id, u64 *data) · line 429

Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +3 -0
+#define TDX_SYSINFO_MAP(_field, _type, _member)			\
+{								\
+	.field_id	= TDX_MD_FIELD_ID_##_field,		\

I find it a bit hard to search the code when it constructs the "TDX_MD_FIELD_ID_" part of the define inside the macro. And since they are all named "TDX_MD_FIELD_ID_FOO" it seems a bit verbose and repetitive. Can we have a smaller pattern such that we can use the exact macro name in TDX_SYSINFO_MAP()? Maybe like this:

Quoted 5 lines

#define TDX_FIELD_MODULE_HV 0x8900000100000000ULL

static const struct field_mapping handoff_mappings[] = {
TDX_SYSINFO_MAP_HANDOFF(TDX_FIELD_MODULE_HV, module_hv),
};

Looks good. Being greppable is a good property. Will do in v3.

#4 lore raw

Re: [RFC PATCH v2 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields

In reply to Chao Gao

arch/x86/virt/vmx/tdx/tdx.c static int read_sys_metadata_field(u64 field_id, u64 *data) · line 407

Quoted 11 lines · Chao Gao
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+/*
+ * Map a TDX global metadata field to a structure member.
+ * @field_id: The TDX global metadata field ID.
+ * @size: The size of the structure member.
+ * @offset: The member's offset within its containing structure.
+ */
+struct field_mapping {
+	u64 field_id;
+	size_t size;
+	int offset;

Nit:
Is it better to align the type of "size" and "offset" with size_t? Since it's already 24 bytes per entry on x86_64, there is no extra memory cost.

Quoted 29 lines
arch/x86/virt/vmx/tdx/tdx.c +26 -0
+};
+
+/* Read each metadata field listed in @mappings[] into @data. */
+static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
+						    int num_mappings, void *data)
+{
+	int i, ret;
+	u64 val;
+
+	for (i = 0; i < num_mappings; i++) {
+		ret = read_sys_metadata_field(mappings[i].field_id, &val);
+		if (ret)
+			return ret;
+		memcpy((char *)data + mappings[i].offset, &val, mappings[i].size);
+	}
+
+	return 0;
+}
+
+#define TDX_SYSINFO_MAP(_field, _type, _member)			\
+{								\
+	.field_id	= TDX_MD_FIELD_ID_##_field,		\
+	.offset		= offsetof(_type, _member),		\
+	.size		= sizeof_field(_type, _member),		\
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
author
#5 lore raw

RFC PATCH v2 2/10 x86/virt/tdx: Convert the version metadata reader

In reply to Chao Gao

With the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.

Convert the version metadata reader: add a table that pairs each field ID with the 'struct tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.

Name the field IDs for readability, so the table entries don't carry raw hex literals.

Also add read_sys_metadata_table() macro to derive the number of entries with ARRAY_SIZE() so that callers don't need to pass it separately.

AI was used under supervision to review code and workshop logs. It suggests adding read_sys_metadata_table() macro.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 21 +++++++++++++++++++--
 arch/x86/virt/vmx/tdx/tdx.h                 | 10 ++++++++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 15 ---------------
 3 files changed, 29 insertions(+), 17 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +19 -2
index 5cd9c6eb98c2..0c8b571f5186 100644
@@ -408,8 +408,8 @@ struct field_mapping {
 };
 
 /* Read each metadata field listed in @mappings[] into @data. */
-static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
-						    int num_mappings, void *data)
+static int __read_sys_metadata_table(const struct field_mapping *mappings,
+				     int num_mappings, void *data)
 {
 	int i, ret;
 	u64 val;
@@ -431,6 +431,23 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *
 	.size		= sizeof_field(_type, _member),		\
 }
 
+#define TDX_SYSINFO_MAP_VERSION(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_version, _member)
+
+static const struct field_mapping version_mappings[] = {
+	TDX_SYSINFO_MAP_VERSION(MINOR_VERSION,  minor_version),
+	TDX_SYSINFO_MAP_VERSION(MAJOR_VERSION,  major_version),
+	TDX_SYSINFO_MAP_VERSION(UPDATE_VERSION, update_version),
+};
+
+#define read_sys_metadata_table(_table, _data) \
+	__read_sys_metadata_table(_table, ARRAY_SIZE(_table), _data)
+
+static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
+{
+	return read_sys_metadata_table(version_mappings, version);
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +10 -0
index a886c54decaa..44124f399703 100644
@@ -60,6 +60,16 @@
  */
 #define TDX_VERSION_SHIFT		16
 
+/*
+ * TDX global metadata field IDs.
+ *
+ * See "global_metadata.pdf" in Intel TDX Module ABI Definitions.
+ */
+/* Class "TDX Module Version" */
+#define TDX_MD_FIELD_ID_MINOR_VERSION			0x0800000100000003ULL
+#define TDX_MD_FIELD_ID_MAJOR_VERSION			0x0800000100000004ULL
+#define TDX_MD_FIELD_ID_UPDATE_VERSION			0x0800000100000005ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -15
index 98ebf17aab1c..9510bf5f6e7d 100644
@@ -7,21 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static int get_tdx_sys_info_version(struct tdx_sys_info_version *sysinfo_version)
-{
-	int ret = 0;
-	u64 val;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
-		sysinfo_version->minor_version = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
-		sysinfo_version->major_version = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
-		sysinfo_version->update_version = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinfo_features)
 {
 	int ret = 0;
Signature · 1 line
2.52.0
#6 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

Quoted 43 lines · Chao Gao

With the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.

Convert the version metadata reader: add a table that pairs each field ID with the 'struct tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.

Name the field IDs for readability, so the table entries don't carry raw hex literals.

Also add read_sys_metadata_table() macro to derive the number of entries with ARRAY_SIZE() so that callers don't need to pass it separately.

AI was used under supervision to review code and workshop logs. It suggests adding read_sys_metadata_table() macro.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 21 +++++++++++++++++++--
 arch/x86/virt/vmx/tdx/tdx.h                 | 10 ++++++++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 15 ---------------
 3 files changed, 29 insertions(+), 17 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +3 -2
index 5cd9c6eb98c2..0c8b571f5186 100644
@@ -408,8 +408,8 @@ struct field_mapping {
 };
 
 /* Read each metadata field listed in @mappings[] into @data. */
-static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
-						    int num_mappings, void *data)
+static int __read_sys_metadata_table(const struct field_mapping *mappings,
+				     int num_mappings, void *data)
 {
 	int i, ret;
 	u64 val;
@@ -431,6 +431,23 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *
 	.size		= sizeof_field(_type, _member),		\
 }
 
+#define TDX_SYSINFO_MAP_VERSION(_field_id, _member) \

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 444

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_version, _member)
+
+static const struct field_mapping version_mappings[] = {
+	TDX_SYSINFO_MAP_VERSION(MINOR_VERSION,  minor_version),
+	TDX_SYSINFO_MAP_VERSION(MAJOR_VERSION,  major_version),
+	TDX_SYSINFO_MAP_VERSION(UPDATE_VERSION, update_version),
+};
+
+#define read_sys_metadata_table(_table, _data) \
+	__read_sys_metadata_table(_table, ARRAY_SIZE(_table), _data)

Should this go in the previous patch?

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 449

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+
+static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
+{
+	return read_sys_metadata_table(version_mappings, version);
+}

I'm not sure we need all of these helpers that are each only called once?

Quoted 51 lines
arch/x86/virt/vmx/tdx/tdx.c +1 -0
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +10 -0
index a886c54decaa..44124f399703 100644
@@ -60,6 +60,16 @@
  */
 #define TDX_VERSION_SHIFT		16
 
+/*
+ * TDX global metadata field IDs.
+ *
+ * See "global_metadata.pdf" in Intel TDX Module ABI Definitions.
+ */
+/* Class "TDX Module Version" */
+#define TDX_MD_FIELD_ID_MINOR_VERSION			0x0800000100000003ULL
+#define TDX_MD_FIELD_ID_MAJOR_VERSION			0x0800000100000004ULL
+#define TDX_MD_FIELD_ID_UPDATE_VERSION			0x0800000100000005ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -15
index 98ebf17aab1c..9510bf5f6e7d 100644
@@ -7,21 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static int get_tdx_sys_info_version(struct tdx_sys_info_version *sysinfo_version)
-{
-	int ret = 0;
-	u64 val;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
-		sysinfo_version->minor_version = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
-		sysinfo_version->major_version = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
-		sysinfo_version->update_version = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinfo_features)
 {
 	int ret = 0;
author
#7 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

Quoted 12 lines
Quoted 12 lines

/* Read each metadata field listed in @mappings[] into @data. */

arch/x86/virt/vmx/tdx/tdx.c +2 -2
-static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *mappings,
-						    int num_mappings, void *data)
+static int __read_sys_metadata_table(const struct field_mapping *mappings,
+				     int num_mappings, void *data)
 {
 	int i, ret;
 	u64 val;
@@ -431,6 +431,23 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *
 	.size		= sizeof_field(_type, _member),		\
 }
 
Quoted 13 lines

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 444

Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +11 -0
+#define TDX_SYSINFO_MAP_VERSION(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_version, _member)
+
+static const struct field_mapping version_mappings[] = {
+	TDX_SYSINFO_MAP_VERSION(MINOR_VERSION,  minor_version),
+	TDX_SYSINFO_MAP_VERSION(MAJOR_VERSION,  major_version),
+	TDX_SYSINFO_MAP_VERSION(UPDATE_VERSION, update_version),
+};
+
+#define read_sys_metadata_table(_table, _data) \
+	__read_sys_metadata_table(_table, ARRAY_SIZE(_table), _data)

Should this go in the previous patch?

I didn't put it there because the previous patch doesn't strictly need it; this patch is the first user.

Adding the macro in the previous patch while still tagging __read_sys_metadata_table() __maybe_unused looks a bit odd:
the macro references the function, yet nothing uses either of them.

That said, I can move it if the oddness doesn't bother you.

Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 449

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+
+static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
+{
+	return read_sys_metadata_table(version_mappings, version);
+}

I'm not sure we need all of these helpers that are each only called once?

I'd prefer to keep them, for a few reasons:

1. This one is actually called twice: from get_tdx_sys_info() and from tdx_module_run_update().

2. The helper name describes what is being read better than a bare read_sys_metadata_table() call.

3. Some helpers do more than read a table (see get_tdx_sys_info_td_conf()). Without per-class helpers, get_tdx_sys_info() would open-code read_sys_metadata_table() for some classes and call a helper for others, which looks inconsistent. With one helper per metadata class, every class is read the same way.

#8 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

Quoted 9 lines · Chao Gao
Quoted 5 lines

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 449

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
+static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
+{
+	return read_sys_metadata_table(version_mappings, version);
+}

I'm not sure we need all of these helpers that are each only called once?

I'd prefer to keep them, for a few reasons:

1. This one is actually called twice: from get_tdx_sys_info() and from tdx_module_run_update().

Neither has any real space constraints, I'll note.

Quoted 2 lines

2. The helper name describes what is being read better than a bare read_sys_metadata_table() call.

What part? that it comes from "sys_info" as opposed to it being "metadata". Meh. Why does it matter?

Quoted 5 lines

3. Some helpers do more than read a table (see get_tdx_sys_info_td_conf()). Without per-class helpers, get_tdx_sys_info() would open-code read_sys_metadata_table() for some classes and call a helper for others, which looks inconsistent. With one helper per metadata class, every class is read the same way.

I value lower levels of abstraction over naming consistency.

Remove the helper, please.

author
#9 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

Quoted 25 lines · Dave Hansen
Quoted 9 lines · Chao Gao
Quoted 5 lines

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 449

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
+static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
+{
+	return read_sys_metadata_table(version_mappings, version);
+}

I'm not sure we need all of these helpers that are each only called once?

I'd prefer to keep them, for a few reasons:

1. This one is actually called twice: from get_tdx_sys_info() and from tdx_module_run_update().

Neither has any real space constraints, I'll note.

Quoted 2 lines

2. The helper name describes what is being read better than a bare read_sys_metadata_table() call.

What part? that it comes from "sys_info" as opposed to it being "metadata". Meh. Why does it matter?

Quoted 5 lines

3. Some helpers do more than read a table (see get_tdx_sys_info_td_conf()). Without per-class helpers, get_tdx_sys_info() would open-code read_sys_metadata_table() for some classes and call a helper for others, which looks inconsistent. With one helper per metadata class, every class is read the same way.

Quoted 3 lines

I value lower levels of abstraction over naming consistency.

Remove the helper, please.

Sure, will do.

Then I would go one step further and move the mapping table into get_tdx_sys_info() too, so it sits next to its only use. Using the "features" metadata class as an example:

static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)

{
	static const struct field_mapping feature_mappings[] __initconst = {
		TDX_SYSINFO_MAP_FEATURES(TDX_FIELD_TDX_FEATURES0, tdx_features0),
	};
	int ret = 0;

	ret = ret ?: read_sys_metadata_table(version_mappings, &sysinfo->version);
	...
	ret = ret ?: read_sys_metadata_table(feature_mappings, &sysinfo->features);

version_mappings[] will be the exception and stay at file scope because tdx_module_run_update() reads it as well.

Let me know if you disagree.

#10 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

Quoted 16 lines · Chao Gao

Then I would go one step further and move the mapping table into get_tdx_sys_info() too, so it sits next to its only use. Using the "features" metadata class as an example:

static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)

{
	static const struct field_mapping feature_mappings[] __initconst = {
		TDX_SYSINFO_MAP_FEATURES(TDX_FIELD_TDX_FEATURES0, tdx_features0),
	};
	int ret = 0;

	ret = ret ?: read_sys_metadata_table(version_mappings, &sysinfo->version);
	...
	ret = ret ?: read_sys_metadata_table(feature_mappings, &sysinfo->features);
Quoted 4 lines

version_mappings[] will be the exception and stay at file scope because tdx_module_run_update() reads it as well.

Let me know if you disagree.

Two concerns: One, the structures are wide and the extra indentation might makes them too wide. Two, this separates the code from the function header. That might hurt function readability.

The value of making a static variable is also lower for something that's const because you don't have writers.

So I don't hate it especially for 3 lines, but I do think it generally looks more natural to have these at global scope.

#11 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

In reply to Chao Gao
Quoted 7 lines · Chao Gao
Quoted 4 lines

arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 444

Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -0
+#define read_sys_metadata_table(_table, _data) \
+	__read_sys_metadata_table(_table, ARRAY_SIZE(_table), _data)

Should this go in the previous patch?

I didn't put it there because the previous patch doesn't strictly need it; this patch is the first user.

Well the functions in the previous patch doesn't have any callers either...

Quoted 4 lines

Adding the macro in the previous patch while still tagging __read_sys_metadata_table() __maybe_unused looks a bit odd:
the macro references the function, yet nothing uses either of them.

Ehh.

Quoted 2 lines

That said, I can move it if the oddness doesn't bother you.

I'd move it because it seems inconsistent to put it here.

#12 lore raw

Re: [RFC PATCH v2 02/10] x86/virt/tdx: Convert the version metadata reader

In reply to Chao Gao
Quoted 7 lines · Chao Gao

With the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.

Convert the version metadata reader: add a table that pairs each field ID with the 'struct tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.

Quoted 7 lines

Name the field IDs for readability, so the table entries don't carry raw hex literals.

Also add read_sys_metadata_table() macro to derive the number of entries with ARRAY_SIZE() so that callers don't need to pass it separately.

AI was used under supervision to review code and workshop logs. It suggests

                                                                        ^

Nit: suggested

Quoted 1 line

adding read_sys_metadata_table() macro.

author
#13 lore raw

RFC PATCH v2 3/10 x86/virt/tdx: Convert the features metadata reader

In reply to Chao Gao

Continue converting the metadata readers to the table-driven helper.

Add a table that pairs each field ID with the 'struct tdx_sys_info_features' member that holds its value, and read all fields by walking that table.

Even though the structure has only one field, add a table anyway for symmetry with the other classes. Adding a field later then becomes a one-line change.

Annotate the table as __initconst as it is referenced only during init.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 12 ++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h                 |  3 +++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 11 -----------
 3 files changed, 15 insertions(+), 11 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +12 -0
index 0c8b571f5186..59e2396fea39 100644
@@ -448,6 +448,18 @@ static int get_tdx_sys_info_version(struct tdx_sys_info_version *version)
 	return read_sys_metadata_table(version_mappings, version);
 }
 
+#define TDX_SYSINFO_MAP_FEATURES(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_features, _member)
+
+static const struct field_mapping feature_mappings[] __initconst = {
+	TDX_SYSINFO_MAP_FEATURES(TDX_FEATURES0, tdx_features0),
+};
+
+static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *features)
+{
+	return read_sys_metadata_table(feature_mappings, features);
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +3 -0
index 44124f399703..582b6ff9a0bb 100644
@@ -70,6 +70,9 @@
 #define TDX_MD_FIELD_ID_MAJOR_VERSION			0x0800000100000004ULL
 #define TDX_MD_FIELD_ID_UPDATE_VERSION			0x0800000100000005ULL
 
+/* Class "TDX Module Info" */
+#define TDX_MD_FIELD_ID_TDX_FEATURES0			0x0A00000300000008ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -11
index 9510bf5f6e7d..615f4d23c5d8 100644
@@ -7,17 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinfo_features)
-{
-	int ret = 0;
-	u64 val;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x0A00000300000008, &val)))
-		sysinfo_features->tdx_features0 = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *sysinfo_tdmr)
 {
 	int ret;
Signature · 1 line
2.52.0
author
#14 lore raw

RFC PATCH v2 4/10 x86/virt/tdx: Convert the tdmr metadata reader

In reply to Chao Gao

Continue converting the metadata readers to the table-driven helper.

The "TDMR info" metadata class has two readers: one for the fields that are always present, and one for the fields that exist only when the module supports Dynamic PAMT.

Add a table for each, both pairing field IDs with the 'struct tdx_sys_info_tdmr' members that hold their values, and read all fields by walking the tables.

Annotate both tables as __initconst since they are referenced only during init.

AI was used under supervision to review the code and workshop the changelog.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 25 +++++++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h                 |  8 ++++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 31 ---------------------
 3 files changed, 33 insertions(+), 31 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +25 -0
index 59e2396fea39..72aed3fc2b25 100644
@@ -460,6 +460,31 @@ static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *featur
 	return read_sys_metadata_table(feature_mappings, features);
 }
 
+#define TDX_SYSINFO_MAP_TDMR(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_tdmr, _member)
+
+static const struct field_mapping tdmr_mappings[] __initconst = {
+	TDX_SYSINFO_MAP_TDMR(MAX_TDMRS,			max_tdmrs),
+	TDX_SYSINFO_MAP_TDMR(MAX_RESERVED_PER_TDMR,	max_reserved_per_tdmr),
+	TDX_SYSINFO_MAP_TDMR(PAMT_4K_ENTRY_SIZE,	pamt_4k_entry_size),
+	TDX_SYSINFO_MAP_TDMR(PAMT_2M_ENTRY_SIZE,	pamt_2m_entry_size),
+	TDX_SYSINFO_MAP_TDMR(PAMT_1G_ENTRY_SIZE,	pamt_1g_entry_size),
+};
+
+static __init int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *tdmr)
+{
+	return read_sys_metadata_table(tdmr_mappings, tdmr);
+}
+
+static const struct field_mapping dpamt_mappings[] __initconst = {
+	TDX_SYSINFO_MAP_TDMR(PAMT_PAGE_BITMAP_ENTRY_BITS, pamt_page_bitmap_entry_bits),
+};
+
+static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *tdmr)
+{
+	return read_sys_metadata_table(dpamt_mappings, tdmr);
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +8 -0
index 582b6ff9a0bb..9a8c32b180e1 100644
@@ -73,6 +73,14 @@
 /* Class "TDX Module Info" */
 #define TDX_MD_FIELD_ID_TDX_FEATURES0			0x0A00000300000008ULL
 
+/* Class "TDMR Info" */
+#define TDX_MD_FIELD_ID_MAX_TDMRS			0x9100000100000008ULL
+#define TDX_MD_FIELD_ID_MAX_RESERVED_PER_TDMR		0x9100000100000009ULL
+#define TDX_MD_FIELD_ID_PAMT_4K_ENTRY_SIZE		0x9100000100000010ULL
+#define TDX_MD_FIELD_ID_PAMT_2M_ENTRY_SIZE		0x9100000100000011ULL
+#define TDX_MD_FIELD_ID_PAMT_1G_ENTRY_SIZE		0x9100000100000012ULL
+#define TDX_MD_FIELD_ID_PAMT_PAGE_BITMAP_ENTRY_BITS	0x9100000000000013ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -31
index 615f4d23c5d8..f0673dc28cb9 100644
@@ -7,37 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *sysinfo_tdmr)
-{
-	int ret;
-	u64 val;
-
-	ret = read_sys_metadata_field(0x9100000000000013, &val);
-	if (!ret)
-		sysinfo_tdmr->pamt_page_bitmap_entry_bits = val;
-
-	return ret;
-}
-
-static __init int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)
-{
-	int ret = 0;
-	u64 val;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x9100000100000008, &val)))
-		sysinfo_tdmr->max_tdmrs = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9100000100000009, &val)))
-		sysinfo_tdmr->max_reserved_per_tdmr = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9100000100000010, &val)))
-		sysinfo_tdmr->pamt_4k_entry_size = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9100000100000011, &val)))
-		sysinfo_tdmr->pamt_2m_entry_size = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9100000100000012, &val)))
-		sysinfo_tdmr->pamt_1g_entry_size = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info_td_ctrl(struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl)
 {
 	int ret = 0;
Signature · 1 line
2.52.0
author
#15 lore raw

RFC PATCH v2 5/10 x86/virt/tdx: Convert the td_ctrl metadata reader

In reply to Chao Gao

Continue converting the metadata readers to the table-driven helper.

Add a table that pairs each field ID with the 'struct tdx_sys_info_td_ctrl' member that holds its value, and read all fields by walking that table.

Annotate the table as __initconst as it is referenced only during init.

AI was used under supervision to review code and workshop logs

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 14 ++++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h                 |  5 +++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 15 ---------------
 3 files changed, 19 insertions(+), 15 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +14 -0
index 72aed3fc2b25..7b6b1ee075a3 100644
@@ -485,6 +485,20 @@ static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *tdmr)
 	return read_sys_metadata_table(dpamt_mappings, tdmr);
 }
 
+#define TDX_SYSINFO_MAP_TD_CTRL(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_td_ctrl, _member)
+
+static const struct field_mapping td_ctrl_mappings[] __initconst = {
+	TDX_SYSINFO_MAP_TD_CTRL(TDR_BASE_SIZE,		tdr_base_size),
+	TDX_SYSINFO_MAP_TD_CTRL(TDCS_BASE_SIZE,		tdcs_base_size),
+	TDX_SYSINFO_MAP_TD_CTRL(TDVPS_BASE_SIZE,	tdvps_base_size),
+};
+
+static __init int get_tdx_sys_info_td_ctrl(struct tdx_sys_info_td_ctrl *td_ctrl)
+{
+	return read_sys_metadata_table(td_ctrl_mappings, td_ctrl);
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +5 -0
index 9a8c32b180e1..f33eb7dc7b45 100644
@@ -81,6 +81,11 @@
 #define TDX_MD_FIELD_ID_PAMT_1G_ENTRY_SIZE		0x9100000100000012ULL
 #define TDX_MD_FIELD_ID_PAMT_PAGE_BITMAP_ENTRY_BITS	0x9100000000000013ULL
 
+/* Class "TD Control Structures" */
+#define TDX_MD_FIELD_ID_TDR_BASE_SIZE			0x9800000100000000ULL
+#define TDX_MD_FIELD_ID_TDCS_BASE_SIZE			0x9800000100000100ULL
+#define TDX_MD_FIELD_ID_TDVPS_BASE_SIZE			0x9800000100000200ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -15
index f0673dc28cb9..c6b375771265 100644
@@ -7,21 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static __init int get_tdx_sys_info_td_ctrl(struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl)
-{
-	int ret = 0;
-	u64 val;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x9800000100000000, &val)))
-		sysinfo_td_ctrl->tdr_base_size = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9800000100000100, &val)))
-		sysinfo_td_ctrl->tdcs_base_size = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9800000100000200, &val)))
-		sysinfo_td_ctrl->tdvps_base_size = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_td_conf)
 {
 	int ret = 0;
Signature · 1 line
2.52.0
author
#16 lore raw

RFC PATCH v2 6/10 x86/virt/tdx: Convert the handoff metadata reader

In reply to Chao Gao

Continue converting the metadata readers to the table-driven helper.

Add a table that pairs each field ID with the 'struct tdx_sys_info_handoff' member that holds its value, and read all fields by walking that table.

AI was used under supervision to review code and workshop logs

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 12 ++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h                 |  3 +++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 -------------
 3 files changed, 15 insertions(+), 13 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +12 -0
index 7b6b1ee075a3..2bd7a2c9432b 100644
@@ -499,6 +499,18 @@ static __init int get_tdx_sys_info_td_ctrl(struct tdx_sys_info_td_ctrl *td_ctrl)
 	return read_sys_metadata_table(td_ctrl_mappings, td_ctrl);
 }
 
+#define TDX_SYSINFO_MAP_HANDOFF(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_handoff, _member)
+
+static const struct field_mapping handoff_mappings[] = {
+	TDX_SYSINFO_MAP_HANDOFF(MODULE_HV, module_hv),
+};
+
+static int get_tdx_sys_info_handoff(struct tdx_sys_info_handoff *handoff)
+{
+	return read_sys_metadata_table(handoff_mappings, handoff);
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +3 -0
index f33eb7dc7b45..6f99e4cdcc19 100644
@@ -86,6 +86,9 @@
 #define TDX_MD_FIELD_ID_TDCS_BASE_SIZE			0x9800000100000100ULL
 #define TDX_MD_FIELD_ID_TDVPS_BASE_SIZE			0x9800000100000200ULL
 
+/* Class "TDX Module Handoff" */
+#define TDX_MD_FIELD_ID_MODULE_HV			0x8900000100000000ULL
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -13
index c6b375771265..36bbf42dead9 100644
@@ -40,19 +40,6 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_
 	return ret;
 }
 
-static int get_tdx_sys_info_handoff(struct tdx_sys_info_handoff *sysinfo_handoff)
-{
-	int ret;
-	u64 val;
-
-	ret = read_sys_metadata_field(0x8900000100000000, &val);
-	if (ret)
-		return ret;
-
-	sysinfo_handoff->module_hv = val;
-	return 0;
-}
-
 static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 {
 	int ret = 0;
Signature · 1 line
2.52.0
author
#17 lore raw

RFC PATCH v2 7/10 x86/virt/tdx: Convert the td_conf metadata reader

In reply to Chao Gao

Continue converting the metadata readers to the table-driven helper.

The "TD Configurability" class holds several scalar fields plus two CPUID arrays. The arrays have a fixed capacity, but the number of entries to read is variable and reported by the num_cpuid_config scalar field.

Add a table that pairs each scalar field ID with the 'struct tdx_sys_info_td_conf' member that holds its value, and read those fields by walking that table.

Annotate the table as __initconst as it is referenced only during init.

Leave the two arrays open coded. 'struct field_mapping' pairs one field ID with one structure member, so describing an array would require a field ID per element plus an entry count that is unknown until num_cpuid_config has been read. That is not worth building for the only two arrays the kernel reads.

Read the arrays with explicit loops as the generated code did, but store each value directly into its array member instead of into a temporary u64 first. The members are u64 already, so the extra copies serve no purpose.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 51 +++++++++++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h                 | 10 ++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 33 -------------
 3 files changed, 61 insertions(+), 33 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +51 -0
index 2bd7a2c9432b..571c3bb50532 100644
@@ -511,6 +511,57 @@ static int get_tdx_sys_info_handoff(struct tdx_sys_info_handoff *handoff)
 	return read_sys_metadata_table(handoff_mappings, handoff);
 }
 
+#define TDX_SYSINFO_MAP_TD_CONF(_field_id, _member) \
+	TDX_SYSINFO_MAP(_field_id, struct tdx_sys_info_td_conf, _member)
+
+static const struct field_mapping td_conf_mappings[] __initconst = {
+	TDX_SYSINFO_MAP_TD_CONF(ATTRIBUTES_FIXED0,	attributes_fixed0),
+	TDX_SYSINFO_MAP_TD_CONF(ATTRIBUTES_FIXED1,	attributes_fixed1),
+	TDX_SYSINFO_MAP_TD_CONF(XFAM_FIXED0,		xfam_fixed0),
+	TDX_SYSINFO_MAP_TD_CONF(XFAM_FIXED1,		xfam_fixed1),
+	TDX_SYSINFO_MAP_TD_CONF(NUM_CPUID_CONFIG,	num_cpuid_config),
+	TDX_SYSINFO_MAP_TD_CONF(MAX_VCPUS_PER_TD,	max_vcpus_per_td),
+};
+
+static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)
+{
+	int ret, i, j;
+
+	ret = read_sys_metadata_table(td_conf_mappings, td_conf);
+	if (ret)
+		return ret;
+
+	/*
+	 * The number of CPUID config entries must not exceed the array
+	 * sizes.
+	 */
+	if (td_conf->num_cpuid_config > ARRAY_SIZE(td_conf->cpuid_config_leaves) ||
+	    td_conf->num_cpuid_config > ARRAY_SIZE(td_conf->cpuid_config_values))
+		return -EINVAL;
+
+	/*
+	 * TDX_MD_FIELD_ID_CPUID_CONFIG_* give the field ID of each array's
+	 * first element. The remaining elements follow consecutively, in
+	 * the order they appear in the structure.
+	 */
+	for (i = 0; i < td_conf->num_cpuid_config; i++) {
+		ret = read_sys_metadata_field(TDX_MD_FIELD_ID_CPUID_CONFIG_LEAVES + i,
+					      &td_conf->cpuid_config_leaves[i]);
+		if (ret)
+			return ret;
+
+		for (j = 0; j < 2; j++) {
+			ret = read_sys_metadata_field(
+				TDX_MD_FIELD_ID_CPUID_CONFIG_VALUES + i * 2 + j,
+				&td_conf->cpuid_config_values[i][j]);
+			if (ret)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
 #include "tdx_global_metadata.c"
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +10 -0
index 6f99e4cdcc19..407aded3137a 100644
@@ -86,6 +86,16 @@
 #define TDX_MD_FIELD_ID_TDCS_BASE_SIZE			0x9800000100000100ULL
 #define TDX_MD_FIELD_ID_TDVPS_BASE_SIZE			0x9800000100000200ULL
 
+/* Class "TD Configurability" */
+#define TDX_MD_FIELD_ID_ATTRIBUTES_FIXED0		0x1900000300000000ULL
+#define TDX_MD_FIELD_ID_ATTRIBUTES_FIXED1		0x1900000300000001ULL
+#define TDX_MD_FIELD_ID_XFAM_FIXED0			0x1900000300000002ULL
+#define TDX_MD_FIELD_ID_XFAM_FIXED1			0x1900000300000003ULL
+#define TDX_MD_FIELD_ID_NUM_CPUID_CONFIG		0x9900000100000004ULL
+#define TDX_MD_FIELD_ID_MAX_VCPUS_PER_TD		0x9900000100000008ULL
+#define TDX_MD_FIELD_ID_CPUID_CONFIG_LEAVES		0x9900000300000400ULL
+#define TDX_MD_FIELD_ID_CPUID_CONFIG_VALUES		0x9900000300000500ULL
+
 /* Class "TDX Module Handoff" */
 #define TDX_MD_FIELD_ID_MODULE_HV			0x8900000100000000ULL
 
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -33
index 36bbf42dead9..779c26e68f02 100644
@@ -7,39 +7,6 @@
  * Include this file to other C file instead.
  */
 
-static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_td_conf)
-{
-	int ret = 0;
-	u64 val;
-	int i, j;
-
-	if (!ret && !(ret = read_sys_metadata_field(0x1900000300000000, &val)))
-		sysinfo_td_conf->attributes_fixed0 = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x1900000300000001, &val)))
-		sysinfo_td_conf->attributes_fixed1 = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x1900000300000002, &val)))
-		sysinfo_td_conf->xfam_fixed0 = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x1900000300000003, &val)))
-		sysinfo_td_conf->xfam_fixed1 = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9900000100000004, &val)))
-		sysinfo_td_conf->num_cpuid_config = val;
-	if (!ret && !(ret = read_sys_metadata_field(0x9900000100000008, &val)))
-		sysinfo_td_conf->max_vcpus_per_td = val;
-	if (sysinfo_td_conf->num_cpuid_config > ARRAY_SIZE(sysinfo_td_conf->cpuid_config_leaves))
-		return -EINVAL;
-	for (i = 0; i < sysinfo_td_conf->num_cpuid_config; i++)
-		if (!ret && !(ret = read_sys_metadata_field(0x9900000300000400 + i, &val)))
-			sysinfo_td_conf->cpuid_config_leaves[i] = val;
-	if (sysinfo_td_conf->num_cpuid_config > ARRAY_SIZE(sysinfo_td_conf->cpuid_config_values))
-		return -EINVAL;
-	for (i = 0; i < sysinfo_td_conf->num_cpuid_config; i++)
-		for (j = 0; j < 2; j++)
-			if (!ret && !(ret = read_sys_metadata_field(0x9900000300000500 + i * 2 + j, &val)))
-				sysinfo_td_conf->cpuid_config_values[i][j] = val;
-
-	return ret;
-}
-
 static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 {
 	int ret = 0;
Signature · 1 line
2.52.0
author
#18 lore raw

RFC PATCH v2 8/10 x86/virt/tdx: Remove tdx_global_metadata.c

In reply to Chao Gao

tdx_global_metadata.c held the script-generated metadata readers, deliberately kept in their own file so that generated code stayed separate from hand-written code. It cannot be compiled on its own because it lacks the low-level SEAMCALL wrappers, so tdx.c #includes it directly.

Including one C file into another is unusual, and now that the readers are maintained by hand there is nothing left to isolate. get_tdx_sys_info() is the only function still in the file.

Move get_tdx_sys_info() verbatim into tdx.c and delete tdx_global_metadata.c along with its #include.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c                 | 28 +++++++++++++++-
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 36 ---------------------
 2 files changed, 27 insertions(+), 37 deletions(-)
 delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c
arch/x86/virt/vmx/tdx/tdx.c +27 -1
index 571c3bb50532..9f9f2ef80f55 100644
@@ -562,7 +562,33 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)
 	return 0;
 }
 
-#include "tdx_global_metadata.c"
+static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
+{
+	int ret = 0;
+
+	ret = ret ?: get_tdx_sys_info_version(&sysinfo->version);
+
+	pr_info("Module version: " TDX_VERSION_FMT "\n",
+		sysinfo->version.major_version,
+		sysinfo->version.minor_version,
+		sysinfo->version.update_version);
+
+	ret = ret ?: get_tdx_sys_info_features(&sysinfo->features);
+	ret = ret ?: get_tdx_sys_info_tdmr(&sysinfo->tdmr);
+	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
+	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+
+	/*
+	 * The kernel supports using TDX without DPAMT, so
+	 * avoid reporting failure if it's not supported. Don't
+	 * try to support buggy TDX modules that advertise
+	 * DPAMT but don't expose the metadata.
+	 */
+	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
+		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
+
+	return ret;
+}
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
 {
arch/x86/virt/vmx/tdx/tdx_global_metadata.c deleted +0 -36
deleted file mode 100644
index 779c26e68f02..000000000000
@@ -1,36 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Functions to read TDX global metadata.
- *
- * This file doesn't compile on its own as it lacks of inclusion
- * of SEAMCALL wrapper primitive which reads global metadata.
- * Include this file to other C file instead.
- */
-
-static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
-{
-	int ret = 0;
-
-	ret = ret ?: get_tdx_sys_info_version(&sysinfo->version);
-
-	pr_info("Module version: " TDX_VERSION_FMT "\n",
-		sysinfo->version.major_version,
-		sysinfo->version.minor_version,
-		sysinfo->version.update_version);
-
-	ret = ret ?: get_tdx_sys_info_features(&sysinfo->features);
-	ret = ret ?: get_tdx_sys_info_tdmr(&sysinfo->tdmr);
-	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
-	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
-
-	/*
-	 * The kernel supports using TDX without DPAMT, so
-	 * avoid reporting failure if it's not supported. Don't
-	 * try to support buggy TDX modules that advertise
-	 * DPAMT but don't expose the metadata.
-	 */
-	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
-		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
-
-	return ret;
-}
Signature · 1 line
2.52.0
maintainer Reviewed-by on v2 8/10
#19 lore raw

Re: [RFC PATCH v2 08/10] x86/virt/tdx: Remove tdx_global_metadata.c

Quoted 9 lines · Chao Gao

tdx_global_metadata.c held the script-generated metadata readers, deliberately kept in their own file so that generated code stayed separate from hand-written code. It cannot be compiled on its own because it lacks the low-level SEAMCALL wrappers, so tdx.c #includes it directly.

Including one C file into another is unusual, and now that the readers are maintained by hand there is nothing left to isolate. get_tdx_sys_info() is the only function still in the file.

Quoted 6 lines

Move get_tdx_sys_info() verbatim into tdx.c and delete tdx_global_metadata.c along with its #include.

AI was used under supervision to review code and workshop logs.

author
#20 lore raw

RFC PATCH v2 9/10 x86/virt/tdx: Use early returns in get_tdx_sys_info()

In reply to Chao Gao

get_tdx_sys_info() was generated by a script. It chains its metadata reads with:

      ret = ret ?: get_tdx_sys_info_foo(...);

The function is maintained by hand now. Use conventional early returns instead.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +25 -8
index 9f9f2ef80f55..9d5a3296d3c0 100644
@@ -564,19 +564,33 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)
 
 static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 {
-	int ret = 0;
+	int ret;
 
-	ret = ret ?: get_tdx_sys_info_version(&sysinfo->version);
+	ret = get_tdx_sys_info_version(&sysinfo->version);
 
 	pr_info("Module version: " TDX_VERSION_FMT "\n",
 		sysinfo->version.major_version,
 		sysinfo->version.minor_version,
 		sysinfo->version.update_version);
 
-	ret = ret ?: get_tdx_sys_info_features(&sysinfo->features);
-	ret = ret ?: get_tdx_sys_info_tdmr(&sysinfo->tdmr);
-	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
-	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_features(&sysinfo->features);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_tdmr(&sysinfo->tdmr);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+	if (ret)
+		return ret;
 
 	/*
 	 * The kernel supports using TDX without DPAMT, so
@@ -584,10 +598,13 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	 * try to support buggy TDX modules that advertise
 	 * DPAMT but don't expose the metadata.
 	 */
-	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
+	if (tdx_supports_dynamic_pamt(sysinfo)) {
 		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
+		if (ret)
+			return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
Signature · 1 line
2.52.0
#21 lore raw

Re: [RFC PATCH v2 09/10] x86/virt/tdx: Use early returns in get_tdx_sys_info()

Quoted 30 lines · Chao Gao

get_tdx_sys_info() was generated by a script. It chains its metadata reads with:

      ret = ret ?: get_tdx_sys_info_foo(...);

The function is maintained by hand now. Use conventional early returns instead.

AI was used under supervision to review code and workshop logs.

---
 arch/x86/virt/vmx/tdx/tdx.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +2 -2
index 9f9f2ef80f55..9d5a3296d3c0 100644
@@ -564,19 +564,33 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)
 
 static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 {
-	int ret = 0;
+	int ret;
 
-	ret = ret ?: get_tdx_sys_info_version(&sysinfo->version);
+	ret = get_tdx_sys_info_version(&sysinfo->version);
 
 	pr_info("Module version: " TDX_VERSION_FMT "\n",

arch/x86/virt/vmx/tdx/tdx.c static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf) · line 577

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -4
 		sysinfo->version.major_version,
 		sysinfo->version.minor_version,
 		sysinfo->version.update_version);
 
-	ret = ret ?: get_tdx_sys_info_features(&sysinfo->features);
-	ret = ret ?: get_tdx_sys_info_tdmr(&sysinfo->tdmr);
-	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
-	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+	if (ret)
+		return ret;

It should not print the module versions if it failed to read them, right? I think the reason why it was done this way was to fit into the existing pattern and because printing zeros is not a horrible outcome. But that code pattern caused problem no longer exists. So I'd think it's ok to fix it in this patch. Before it kind of blended in. But now it easily stands out as buggy.

Quoted 35 lines
arch/x86/virt/vmx/tdx/tdx.c +21 -2
+
+	ret = get_tdx_sys_info_features(&sysinfo->features);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_tdmr(&sysinfo->tdmr);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
+	if (ret)
+		return ret;
+
+	ret = get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+	if (ret)
+		return ret;
 
 	/*
 	 * The kernel supports using TDX without DPAMT, so
@@ -584,10 +598,13 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	 * try to support buggy TDX modules that advertise
 	 * DPAMT but don't expose the metadata.
 	 */
-	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
+	if (tdx_supports_dynamic_pamt(sysinfo)) {
 		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
+		if (ret)
+			return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static __init int check_features(struct tdx_sys_info *sysinfo)
author
#22 lore raw

Re: [RFC PATCH v2 09/10] x86/virt/tdx: Use early returns in get_tdx_sys_info()

Quoted 13 lines
Quoted 13 lines

static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) {

arch/x86/virt/vmx/tdx/tdx.c +2 -2
-	int ret = 0;
+	int ret;
 
-	ret = ret ?: get_tdx_sys_info_version(&sysinfo->version);
+	ret = get_tdx_sys_info_version(&sysinfo->version);
 
 	pr_info("Module version: " TDX_VERSION_FMT "\n",
 		sysinfo->version.major_version,
 		sysinfo->version.minor_version,
 		sysinfo->version.update_version);
Quoted 12 lines

arch/x86/virt/vmx/tdx/tdx.c static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf) · line 577

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -4
-	ret = ret ?: get_tdx_sys_info_features(&sysinfo->features);
-	ret = ret ?: get_tdx_sys_info_tdmr(&sysinfo->tdmr);
-	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
-	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+	if (ret)
+		return ret;

It should not print the module versions if it failed to read them, right? I think the reason why it was done this way was to fit into the existing pattern and because printing zeros is not a horrible outcome. But that code pattern caused problem no longer exists. So I'd think it's ok to fix it in this patch. Before it kind of blended in. But now it easily stands out as buggy.

Right. I'll move the error check before the pr_info() and mention it in the changelog.

author
#23 lore raw

RFC PATCH v2 10/10 x86/virt/tdx: Verify structure member sizes against metadata field IDs

In reply to Chao Gao

A metadata field ID encodes the size of a single element. TDX_SYSINFO_MAP() instead derives the copy size from the destination member, and nothing verifies that the two sizes agree.

A wrongly typed member is a kernel bug: declaring a u32 for an 8-byte metadata field would silently store only its low 4 bytes.

Add macros to extract the element size encoded in a field ID and verify it against the destination member size at build time.

BUILD_BUG_ON() cannot be used in a structure initializer, so use BUILD_BUG_ON_ZERO() and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.

AI was used under supervision to review code and workshop logs. It suggested extracting TDX_MD_FIELD_SIZE_CHECK() instead of open coding the check in TDX_SYSINFO_MAP(), to keep the .size line from being too long.

---
 arch/x86/virt/vmx/tdx/tdx.c | 12 +++++++++++-
 arch/x86/virt/vmx/tdx/tdx.h | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +11 -1
index 9d5a3296d3c0..6dc328561009 100644
@@ -424,11 +424,21 @@ static int __read_sys_metadata_table(const struct field_mapping *mappings,
 	return 0;
 }
 
+/*
+ * The size encoded in the field ID and the size of the destination C
+ * member must agree.
+ */
+#define TDX_MD_FIELD_SIZE_CHECK(_field, _type, _member)	\
+	BUILD_BUG_ON_ZERO(sizeof_field(_type, _member) !=	\
+			  TDX_MD_FIELD_ELE_SIZE(TDX_MD_FIELD_ID_##_field))
+
 #define TDX_SYSINFO_MAP(_field, _type, _member)			\
 {								\
 	.field_id	= TDX_MD_FIELD_ID_##_field,		\
 	.offset		= offsetof(_type, _member),		\
-	.size		= sizeof_field(_type, _member),		\
+	.size		= sizeof_field(_type, _member) +	\
+			  TDX_MD_FIELD_SIZE_CHECK(		\
+				_field, _type, _member),	\
 }
 
 #define TDX_SYSINFO_MAP_VERSION(_field_id, _member) \
arch/x86/virt/vmx/tdx/tdx.h +15 -0
index 407aded3137a..17fdb682410e 100644
@@ -99,6 +99,21 @@
 /* Class "TDX Module Handoff" */
 #define TDX_MD_FIELD_ID_MODULE_HV			0x8900000100000000ULL
 
+/*
+ * Sub-field definitions of TDX global metadata field IDs.
+ *
+ * See "Metadata Field Identifier" in the Intel TDX Module ABI
+ * Specification.
+ *
+ *  - Bit 33:32: ELEMENT_SIZE_CODE -- log2 of a single metadata
+ *                                    element's size in bytes
+ */
+#define TDX_MD_FIELD_ELE_SIZE_CODE(field_id)	\
+	(((field_id) & GENMASK_ULL(33, 32)) >> 32)
+
+#define TDX_MD_FIELD_ELE_SIZE(field_id)	\
+	(1 << TDX_MD_FIELD_ELE_SIZE_CODE(field_id))
+
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
Signature · 1 line
2.52.0
#24 lore raw

Re: [RFC PATCH v2 10/10] x86/virt/tdx: Verify structure member sizes against metadata field IDs

Quoted 9 lines · Chao Gao

A metadata field ID encodes the size of a single element. TDX_SYSINFO_MAP() instead derives the copy size from the destination member, and nothing verifies that the two sizes agree.

A wrongly typed member is a kernel bug: declaring a u32 for an 8-byte metadata field would silently store only its low 4 bytes.

Add macros to extract the element size encoded in a field ID and verify it against the destination member size at build time.

The two things we could do are extract the field code and check it, or add it into the field automatically from the struct size. In the later case the field id's would be specified without the size bits already filled in. But since the metadata docs have the field code already embedded when they are listed in the docs, that is the most natural and easy thing to add to the field id code. It makes it easy to search the docs too. So the checking design gives us some extra safety, make it easier to add the code and search the docs.

I think probably you need to explain a bit more about what and why the fieldid size bits exist, but I agree with the design.

Quoted 4 lines

BUILD_BUG_ON() cannot be used in a structure initializer, so use BUILD_BUG_ON_ZERO() and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.

Neat. Might be controversial. I like it.

Quoted 4 lines

AI was used under supervision to review code and workshop logs. It suggested extracting TDX_MD_FIELD_SIZE_CHECK() instead of open coding the check in TDX_SYSINFO_MAP(), to keep the .size line from being too long.

Oh yea that is also a lot of bit math to cram in a struct member line too.

Quoted 47 lines
---
 arch/x86/virt/vmx/tdx/tdx.c | 12 +++++++++++-
 arch/x86/virt/vmx/tdx/tdx.h | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +11 -1
index 9d5a3296d3c0..6dc328561009 100644
@@ -424,11 +424,21 @@ static int __read_sys_metadata_table(const struct field_mapping *mappings,
 	return 0;
 }
 
+/*
+ * The size encoded in the field ID and the size of the destination C
+ * member must agree.
+ */
+#define TDX_MD_FIELD_SIZE_CHECK(_field, _type, _member)	\
+	BUILD_BUG_ON_ZERO(sizeof_field(_type, _member) !=	\
+			  TDX_MD_FIELD_ELE_SIZE(TDX_MD_FIELD_ID_##_field))
+
 #define TDX_SYSINFO_MAP(_field, _type, _member)			\
 {								\
 	.field_id	= TDX_MD_FIELD_ID_##_field,		\
 	.offset		= offsetof(_type, _member),		\
-	.size		= sizeof_field(_type, _member),		\
+	.size		= sizeof_field(_type, _member) +	\
+			  TDX_MD_FIELD_SIZE_CHECK(		\
+				_field, _type, _member),	\
 }
 
 #define TDX_SYSINFO_MAP_VERSION(_field_id, _member) \
arch/x86/virt/vmx/tdx/tdx.h +5 -0
index 407aded3137a..17fdb682410e 100644
@@ -99,6 +99,21 @@
 /* Class "TDX Module Handoff" */
 #define TDX_MD_FIELD_ID_MODULE_HV			0x8900000100000000ULL
 
+/*
+ * Sub-field definitions of TDX global metadata field IDs.
+ *
+ * See "Metadata Field Identifier" in the Intel TDX Module ABI
+ * Specification.

arch/x86/virt/vmx/tdx/tdx.h line 116

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.h +10 -0
+ *
+ *  - Bit 33:32: ELEMENT_SIZE_CODE -- log2 of a single metadata
+ *                                    element's size in bytes
+ */
+#define TDX_MD_FIELD_ELE_SIZE_CODE(field_id)	\
+	(((field_id) & GENMASK_ULL(33, 32)) >> 32)
+
+#define TDX_MD_FIELD_ELE_SIZE(field_id)	\
+	(1 << TDX_MD_FIELD_ELE_SIZE_CODE(field_id))
+

I'd think these could be squashed together since only TDX_MD_FIELD_ELE_SIZE gets used anywhere else.

Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.h +0 -0
 /* TDX page types */
 #define	PT_NDA		0x0
 #define	PT_RSVD		0x1
author
#25 lore raw

Re: [RFC PATCH v2 10/10] x86/virt/tdx: Verify structure member sizes against metadata field IDs

Quoted 11 lines · Rick P
Quoted 9 lines · Chao Gao

A metadata field ID encodes the size of a single element. TDX_SYSINFO_MAP() instead derives the copy size from the destination member, and nothing verifies that the two sizes agree.

A wrongly typed member is a kernel bug: declaring a u32 for an 8-byte metadata field would silently store only its low 4 bytes.

Add macros to extract the element size encoded in a field ID and verify it against the destination member size at build time.

Quoted 7 lines

The two things we could do are extract the field code and check it, or add it into the field automatically from the struct size. In the later case the field id's would be specified without the size bits already filled in. But since the metadata docs have the field code already embedded when they are listed in the docs, that is the most natural and easy thing to add to the field id code. It makes it easy to search the docs too. So the checking design gives us some extra safety, make it easier to add the code and search the docs.

I'll explain in the changelog why we extract the size from the field ID and check it, rather than building the field ID from the member size.

Quoted 3 lines

I think probably you need to explain a bit more about what and why the fieldid size bits exist, but I agree with the design.

Sure. I will add:

TDH.SYS.RD returns every field's value as a u64, but metadata fields aren't all 64 bits wide. A field ID encodes the size of one field (bits 33:32), so the ID alone gives the field's width.

As for why the size bits exist at all, from the TDX module source it looks like the module itself uses them: it stores array fields packed in memory and uses the size to locate each element (offset = index * size). The kernel doesn't need them except for the size check in this patch.

Quoted 11 lines
Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.h +10 -0
+/*
+ * Sub-field definitions of TDX global metadata field IDs.
+ *
+ * See "Metadata Field Identifier" in the Intel TDX Module ABI
+ * Specification.
+ *
+ *  - Bit 33:32: ELEMENT_SIZE_CODE -- log2 of a single metadata
+ *                                    element's size in bytes
+ */
+#define TDX_MD_FIELD_ELE_SIZE_CODE(field_id)	\
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.h line 116

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.h +5 -0
+	(((field_id) & GENMASK_ULL(33, 32)) >> 32)
+
+#define TDX_MD_FIELD_ELE_SIZE(field_id)	\
+	(1 << TDX_MD_FIELD_ELE_SIZE_CODE(field_id))
+

I'd think these could be squashed together since only TDX_MD_FIELD_ELE_SIZE gets used anywhere else.

Sure. Will do.

#26 lore raw
In reply to Chao Gao
Quoted 12 lines · Chao Gao

Chao Gao (10):
  x86/virt/tdx: Add a helper to read a table of metadata fields x86/virt/tdx: Convert the version metadata reader x86/virt/tdx: Convert the features metadata reader x86/virt/tdx: Convert the tdmr metadata reader x86/virt/tdx: Convert the td_ctrl metadata reader x86/virt/tdx: Convert the handoff metadata reader x86/virt/tdx: Convert the td_conf metadata reader x86/virt/tdx: Remove tdx_global_metadata.c
  x86/virt/tdx: Use early returns in get_tdx_sys_info() x86/virt/tdx: Verify structure member sizes against metadata field IDs

Quoted 8 lines

 arch/x86/virt/vmx/tdx/tdx.c                 | 222 +++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h                 |  54 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 154 --------------

 3 files changed, 275 insertions(+), 155 deletions(-)

 delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c

--
2.52.0

What is the base commit?

author
#27 lore raw
Quoted 19 lines · Rick P
Quoted 18 lines · Chao Gao

Chao Gao (10):
  x86/virt/tdx: Add a helper to read a table of metadata fields x86/virt/tdx: Convert the version metadata reader x86/virt/tdx: Convert the features metadata reader x86/virt/tdx: Convert the tdmr metadata reader x86/virt/tdx: Convert the td_ctrl metadata reader x86/virt/tdx: Convert the handoff metadata reader x86/virt/tdx: Convert the td_conf metadata reader x86/virt/tdx: Remove tdx_global_metadata.c
  x86/virt/tdx: Use early returns in get_tdx_sys_info() x86/virt/tdx: Verify structure member sizes against metadata field IDs

 arch/x86/virt/vmx/tdx/tdx.c                 | 222 +++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h                 |  54 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 154 --------------

 3 files changed, 275 insertions(+), 155 deletions(-)

 delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c

Quoted 4 lines
Quoted 2 lines

--
2.52.0

What is the base commit?

Commit e61c837817ac ("Documentation/x86: Add documentation for TDX's Dynamic PAMT") from the tip/x86/tdx branch.

#28 lore raw
In reply to Chao Gao
Quoted 7 lines · Chao Gao

This series cleans up the TDX global metadata code. It has two goals:

1. Replace the generated code with a table-driven metadata reader.

2. Make the existing code easier to read and maintain, and simplify adding new metadata fields.

Quoted 7 lines

During the v1 review, Dave raised concerns about signing off on AI-generated code. I have since rewritten the affected patches based on my own understanding of the code.

The main goal of this RFC is to agree on whether the table-driven reader is the right replacement for the generated code. Please raise any concerns or alternative design ideas.

For alternatives, I wonder if grouping the metadata would help in the long run. Some of TDX metadata needs to be cached. Some of the metadata is init time data for how much memory to allocate etc. Some of the metadata needs to be re-read after TDX module update.

Should we have the table-driven metadata reader use the TDX module metadata directly by default instead of always copying the data? That could be used for the init time data. And data could still be cached for CPUIDs etc.

author
#29 lore raw
Quoted 12 lines · Tony Lindgren
Quoted 11 lines · Chao Gao

This series cleans up the TDX global metadata code. It has two goals:

1. Replace the generated code with a table-driven metadata reader.

2. Make the existing code easier to read and maintain, and simplify adding new metadata fields.

During the v1 review, Dave raised concerns about signing off on AI-generated code. I have since rewritten the affected patches based on my own understanding of the code.

Quoted 8 lines
Quoted 3 lines

The main goal of this RFC is to agree on whether the table-driven reader is the right replacement for the generated code. Please raise any concerns or alternative design ideas.

For alternatives, I wonder if grouping the metadata would help in the long run. Some of TDX metadata needs to be cached. Some of the metadata is init time data for how much memory to allocate etc. Some of the metadata needs to be re-read after TDX module update.

I agree that TDX metadata should be cached only when there is a reason to.

And yes, some init-time data is cached even though nothing reads it after
init: max_tdmrs, max_reserved_per_tdmr and the pamt_*_entry_size fields are only needed while building the TDMRs.

Only the version fields are re-read after a module update. Splitting them out of struct tdx_sys_info would be a good improvement, so the rest of the structure could then be __ro_after_init.

These are cleanups that can be done in a separate series.

Quoted 5 lines

Should we have the table-driven metadata reader use the TDX module metadata directly by default instead of always copying the data? That could be used for the init time data. And data could still be cached for CPUIDs etc.

The table-driven reader doesn't impose caching. It maps field IDs to members of whatever structure the caller passes, so the destination can be a cached global or a stack local. For init-time data, we can read it into a stack local the same way the handoff class is read in tdx_module_shutdown().

#30 lore raw
Quoted 28 lines · Chao Gao
Quoted 12 lines · Tony Lindgren
Quoted 11 lines · Chao Gao

This series cleans up the TDX global metadata code. It has two goals:

1. Replace the generated code with a table-driven metadata reader.

2. Make the existing code easier to read and maintain, and simplify adding new metadata fields.

During the v1 review, Dave raised concerns about signing off on AI-generated code. I have since rewritten the affected patches based on my own understanding of the code.

Quoted 8 lines
Quoted 3 lines

The main goal of this RFC is to agree on whether the table-driven reader is the right replacement for the generated code. Please raise any concerns or alternative design ideas.

For alternatives, I wonder if grouping the metadata would help in the long run. Some of TDX metadata needs to be cached. Some of the metadata is init time data for how much memory to allocate etc. Some of the metadata needs to be re-read after TDX module update.

I agree that TDX metadata should be cached only when there is a reason to.

And yes, some init-time data is cached even though nothing reads it after
init: max_tdmrs, max_reserved_per_tdmr and the pamt_*_entry_size fields are only needed while building the TDMRs.

Quoted 5 lines

Only the version fields are re-read after a module update. Splitting them out of struct tdx_sys_info would be a good improvement, so the rest of the structure could then be __ro_after_init.

These are cleanups that can be done in a separate series.

Yes OK agreed.

Quoted 9 lines
Quoted 4 lines

Should we have the table-driven metadata reader use the TDX module metadata directly by default instead of always copying the data? That could be used for the init time data. And data could still be cached for CPUIDs etc.

The table-driven reader doesn't impose caching. It maps field IDs to members of whatever structure the caller passes, so the destination can be a cached global or a stack local. For init-time data, we can read it into a stack local the same way the handoff class is read in tdx_module_shutdown().

OK thanks, nice to hear you have already considered the next steps. Might be worth updating the cover letter for the follow up options too.