Skip to contentSkip to conversation

tdx-guest: Make Quote buffer size dynamic

PATCH Superseded Peter Fang 6 participants 22 messages Jun 12 – Jul 17

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
author
#12 lore raw

PATCH 2/2 virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4
---
 drivers/virt/coco/tdx-guest/tdx-guest.c | 52 ++++++++++++++++++-------
 1 file changed, 38 insertions(+), 14 deletions(-)
drivers/virt/coco/tdx-guest/tdx-guest.c +38 -14
index a9ecc46df187..162fb47f3fae 100644
@@ -163,7 +163,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
  * DICE-based attestation uses layered evidence that requires
  * larger Quote size (~100K).
  */
-#define GET_QUOTE_BUF_SIZE		SZ_128K
+#define GET_QUOTE_DEFAULT_BUF_SIZE	SZ_128K
 
 #define GET_QUOTE_CMD_VER		1
 
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))
 
 /* struct tdx_quote_buf: Format of Quote request buffer.
  * @version: Quote format version, filled by TD.
@@ -192,8 +192,9 @@ struct tdx_quote_buf {
 	u8 data[];
 };
 
-/* Quote data buffer */
+/* Quote data buffer and size */
 static void *quote_data;
+static size_t quote_data_size;
 
 /* Lock to streamline quote requests */
 static DEFINE_MUTEX(quote_lock);
@@ -210,9 +211,8 @@ static long tdx_get_report0(struct tdx_report_req __user *req)
 			     USER_SOCKPTR(req->tdreport));
 }
 
-static void free_quote_buf(void *buf)
+static void free_quote_buf(void *buf, size_t len)
 {
-	size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
 	unsigned int count = len >> PAGE_SHIFT;
 
 	if (set_memory_encrypted((unsigned long)buf, count)) {
@@ -223,19 +223,43 @@ static void free_quote_buf(void *buf)
 	free_pages_exact(buf, len);
 }
 
-static void *alloc_quote_buf(void)
+static size_t get_quote_buf_size(void)
 {
-	size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
-	unsigned int count = len >> PAGE_SHIFT;
+	size_t buf_sz = GET_QUOTE_DEFAULT_BUF_SIZE;
+	u32 quote_sz;
+
+	quote_sz = tdx_get_max_quote_size();
+
+	if (quote_sz)
+		/* Reported size does not include GetQuote header */
+		buf_sz = TDX_QUOTE_BUF_LEN(quote_sz);
+
+	return PAGE_ALIGN(buf_sz);
+}
+
+static void *alloc_quote_buf(size_t *buflen)
+{
+	unsigned int count;
+	size_t len;
 	void *addr;
 
+	len = get_quote_buf_size();
+
+	/*
+	 * This fails if the requested size exceeds the buddy allocator's
+	 * maximum order (order-10, 4MB).
+	 */
 	addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);
 	if (!addr)
 		return NULL;
 
+	count = len >> PAGE_SHIFT;
+
 	if (set_memory_decrypted((unsigned long)addr, count))
 		return NULL;
 
+	*buflen = len;
+
 	return addr;
 }
 
@@ -286,7 +310,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (desc->inblob_len != TDX_REPORTDATA_LEN)
 		return -EINVAL;
 
-	memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
+	memset(quote_data, 0, quote_data_size);
 
 	/* Update Quote buffer header */
 	quote_buf->version = GET_QUOTE_CMD_VER;
@@ -297,7 +321,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (ret)
 		return ret;
 
-	err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
+	err = tdx_hcall_get_quote(quote_data, quote_data_size);
 	if (err) {
 		pr_err("GetQuote hypercall failed, status:%llx\n", err);
 		return -EIO;
@@ -316,7 +340,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 
 	out_len = READ_ONCE(quote_buf->out_len);
 
-	if (out_len > TDX_QUOTE_MAX_LEN)
+	if (TDX_QUOTE_BUF_LEN(out_len) > quote_data_size)
 		return -EFBIG;
 
 	buf = kvmemdup(quote_buf->data, out_len, GFP_KERNEL);
@@ -418,7 +442,7 @@ static int __init tdx_guest_init(void)
 	if (ret)
 		goto deinit_mr;
 
-	quote_data = alloc_quote_buf();
+	quote_data = alloc_quote_buf(&quote_data_size);
 	if (!quote_data) {
 		pr_err("Failed to allocate Quote buffer\n");
 		ret = -ENOMEM;
@@ -432,7 +456,7 @@ static int __init tdx_guest_init(void)
 	return 0;
 
 free_quote:
-	free_quote_buf(quote_data);
+	free_quote_buf(quote_data, quote_data_size);
 free_misc:
 	misc_deregister(&tdx_misc_dev);
 deinit_mr:
@@ -445,7 +469,7 @@ module_init(tdx_guest_init);
 static void __exit tdx_guest_exit(void)
 {
 	tsm_report_unregister(&tdx_tsm_ops);
-	free_quote_buf(quote_data);
+	free_quote_buf(quote_data, quote_data_size);
 	misc_deregister(&tdx_misc_dev);
 	tdx_mr_deinit(tdx_attr_groups[0]);
 }
Signature · 1 line
2.53.0

Peter Fang

author
#0 lore raw

cover tdx-guest: Make Quote buffer size dynamic

Hi,

This series changes the TDX attestation driver's Quote buffer size from a fixed constant to a value queried from the TDX module. So effectively:

s/FIXED_BUF_SIZE/queried_buf_size/g

...in the TDX guest driver.

Terminology
===========

A "TD Quote" is an attestation structure signed with a platform key. It contains information about a TDX guest and the platform it's running on.

The "Quote buffer" in the TDX guest driver is a memory buffer shared between the TDX guest and the host VMM to retrieve TD Quotes. It has a header defined in the GHCI spec [1].

Device Identifier Composition Engine ("DICE") provides a framework for layering attestation evidence. This replaces the SGX model of contacting an Intel server to obtain a certificate.

Problem
=======

The fixed-size Quote buffer approach is not sustainable. As cryptographic algorithms evolve, TD Quote sizes also grow. A previous commit [2] increased the guest driver's fixed-size Quote buffer to 128 KB to accommodate DICE Quotes, but it may still be insufficient when those Quotes use post-quantum cryptography (PQC). PQC certificate chains are roughly 10x-15x larger than conventional ones, which can increase Quote sizes to several megabytes.

What's in this series
=====================

To avoid changing the driver whenever the Quote buffer becomes too small, newer TDX modules report their maximum Quote size via a metadata field. The guest driver uses this value for its Quote buffer when available. Older TDX modules continue to use the 128 KB buffer.

The changes do not affect configfs-tsm-report ABIs.

Patch 1/2: Add a helper to read the QUOTE_MAX_SIZE metadata field. Patch 2/2: Replace the fixed Quote buffer size with the queried value,
when available.

AI use
======

I used AI tools (Claude:claude-opus-4-7, GitHub Copilot:gpt-5.4) to proofread this cover letter and the changelogs. The series also underwent AI code review (Claude:claude-opus-4-7), but the feedback was limited to style suggestions.

[1] Guest Hypervisor Communication Interface (GHCI) Specification, Version 1.5, Section "TDG.VP.VMCALL<GetQuote>"
[2] 43185067c6fd ("configfs-tsm-report: tdx_guest: Increase Quote buffer

    size to 128KB")

Kuppuswamy Sathyanarayanan (1):
virt: tdx-guest: Allocate Quote buffer dynamically

Peter Fang (1):
x86/tdx: Add helper to query maximum TD Quote size

 arch/x86/coco/tdx/tdx.c                 | 19 +++++++++
 arch/x86/include/asm/shared/tdx.h       |  1 +
 arch/x86/include/asm/tdx.h              |  2 +
 drivers/virt/coco/tdx-guest/tdx-guest.c | 52 ++++++++++++++++++-------
 4 files changed, 60 insertions(+), 14 deletions(-)

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/2 x86/tdx: Add helper to query maximum TD Quote size

TDX attestation blob ("TD Quote") sizes can grow with newer cryptographic schemes, so guests can no longer rely on a fixed-size buffer for the Quote.

Newer TDX modules report the maximum TD Quote size via a TD-scope metadata field. Add a helper to query it instead of exposing tdg_vm_rd() directly, as it can read arbitrary metadata fields.

Thanks to Xu Yilun for suggesting this.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4
---
 arch/x86/coco/tdx/tdx.c           | 19 +++++++++++++++++++
 arch/x86/include/asm/shared/tdx.h |  1 +
 arch/x86/include/asm/tdx.h        |  2 ++
 3 files changed, 22 insertions(+)
arch/x86/coco/tdx/tdx.c +19 -0
index 186915a17c50..88c66c46e70a 100644
@@ -197,6 +197,25 @@ u64 tdx_hcall_get_quote(u8 *buf, size_t size)
 }
 EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
 
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.
+ */
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);
+
 static void __noreturn tdx_panic(const char *msg)
 {
 	struct tdx_module_args args = {
arch/x86/include/asm/shared/tdx.h +1 -0
index 049638e3da74..2880f493a8e5 100644
@@ -49,6 +49,7 @@
 /* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
 #define TDCS_CONFIG_FLAGS		0x1110000300000016
 #define TDCS_TD_CTLS			0x1110000300000017
+#define TDCS_QUOTE_MAX_SIZE		0x9010000200000008
 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
 #define TDCS_TOPOLOGY_ENUM_CONFIGURED	0x9100000000000019
 
arch/x86/include/asm/tdx.h +2 -0
index a149740b24e8..ac39674c9479 100644
@@ -72,6 +72,8 @@ int tdx_mcall_extend_rtmr(u8 index, u8 *data);
 
 u64 tdx_hcall_get_quote(u8 *buf, size_t size);
 
+u32 tdx_get_max_quote_size(void);
+
 void __init tdx_dump_attributes(u64 td_attr);
 void __init tdx_dump_td_ctls(u64 td_ctls);
 
Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Peter Fang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unbounded host-provided quote size causes guest kernel warning (and potential panic) during…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unbounded host-provided quote size causes guest kernel warning (and potential panic) during allocation. --

commit 918e9fdc48e5a03b41dab3a999be1a3f631327c1
Author: Kuppuswamy Sathyanarayanan <[email protected]>

virt: tdx-guest: Allocate Quote buffer dynamically

This commit modifies the TDX attestation driver to allocate the Quote buffer dynamically based on the size reported by the TDX module. It falls back to a default 128 KB buffer if the reported size is unavailable, supporting larger quotes needed for newer schemes.

Quoted 3 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +0 -0

[ ... ]

Quoted 24 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +16 -3
@@ -223,19 +223,43 @@ static void free_quote_buf(void *buf)
 	free_pages_exact(buf, len);
 }
 
-static void *alloc_quote_buf(void)
+static size_t get_quote_buf_size(void)
 {
-	size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
-	unsigned int count = len >> PAGE_SHIFT;
+	size_t buf_sz = GET_QUOTE_DEFAULT_BUF_SIZE;
+	u32 quote_sz;
+
+	quote_sz = tdx_get_max_quote_size();
+
+	if (quote_sz)
+		/* Reported size does not include GetQuote header */
+		buf_sz = TDX_QUOTE_BUF_LEN(quote_sz);
+
+	return PAGE_ALIGN(buf_sz);
+}
+
+static void *alloc_quote_buf(size_t *buflen)
+{
+	unsigned int count;

drivers/virt/coco/tdx-guest/tdx-guest.c static void free_quote_buf(void *buf) · line 252

Quoted 10 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +7 -0
+	size_t len;
 	void *addr;
 
+	len = get_quote_buf_size();
+
+	/*
+	 * This fails if the requested size exceeds the buddy allocator's
+	 * maximum order (order-10, 4MB).
+	 */
 	addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);

[Severity: High]
If the host provides an excessively large quote_sz, could this allocation trigger a kernel warning?

The comments indicate the buddy allocator will fail requests exceeding its maximum order. However, passing an unbounded, host-controlled size directly to alloc_pages_exact() without __GFP_NOWARN will trigger a WARN_ON_ONCE_GFP in the page allocator's slow path before failing.

If a host is malicious or misconfigured, could this be used to spam the guest's kernel log or cause a denial-of-service crash if the guest runs with panic_on_warn=1?

Would it be better to explicitly check the size against a reasonable upper bound or include the __GFP_NOWARN flag here?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Peter Fang
Quoted 4 lines · Peter Fang

TDX attestation blob ("TD Quote") sizes can grow with newer cryptographic schemes, so guests can no longer rely on a fixed-size buffer for the Quote.

Quoted 8 lines

Newer TDX modules report the maximum TD Quote size via a TD-scope metadata field. Add a helper to query it instead of exposing tdg_vm_rd() directly, as it can read arbitrary metadata fields.

Thanks to Xu Yilun for suggesting this.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

These supposes to be on the same line, no?

Documentation/process/coding-assistants.rst: Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]

Quoted 1 line

One nit below, otherwise:

Quoted 20 lines
---
 arch/x86/coco/tdx/tdx.c           | 19 +++++++++++++++++++
 arch/x86/include/asm/shared/tdx.h |  1 +
 arch/x86/include/asm/tdx.h        |  2 ++
 3 files changed, 22 insertions(+)
arch/x86/coco/tdx/tdx.c +6 -0
index 186915a17c50..88c66c46e70a 100644
@@ -197,6 +197,25 @@ u64 tdx_hcall_get_quote(u8 *buf, size_t size)
 }
 EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
 
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 215

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.
+ */
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;

Cast is redundant.

Quoted 6 lines
arch/x86/coco/tdx/tdx.c +3 -0
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);
+
 static void __noreturn tdx_panic(const char *msg)
 {
 	struct tdx_module_args args = {
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
maintainer Reviewed-by on v1 2/2
#15 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Peter Fang

drivers/virt/coco/tdx-guest/tdx-guest.c static void tdx_mr_deinit(const struct attribute_group *mr_grp) · line 174

Quoted 6 lines · Peter Fang
drivers/virt/coco/tdx-guest/tdx-guest.c +1 -1
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))

I've got confused by this offsetof(). It is valid, but why not plain sizeof()?

Otherwise looks okay to me:

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
Reviewed-by on v1 1/2
#6 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Peter Fang
Quoted 8 lines · Peter Fang

TDX attestation blob ("TD Quote") sizes can grow with newer cryptographic schemes, so guests can no longer rely on a fixed-size buffer for the Quote.

Newer TDX modules report the maximum TD Quote size via a TD-scope metadata field. Add a helper to query it instead of exposing tdg_vm_rd() directly, as it can read arbitrary metadata fields.

Quoted 5 lines

Thanks to Xu Yilun for suggesting this.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

I have another nit other than Kiryl's

Quoted 22 lines
---
  arch/x86/coco/tdx/tdx.c           | 19 +++++++++++++++++++
  arch/x86/include/asm/shared/tdx.h |  1 +
  arch/x86/include/asm/tdx.h        |  2 ++
  3 files changed, 22 insertions(+)
arch/x86/coco/tdx/tdx.c +8 -0
index 186915a17c50..88c66c46e70a 100644
@@ -197,6 +197,25 @@ u64 tdx_hcall_get_quote(u8 *buf, size_t size)
  }
  EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
 
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 217

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+ */
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);

Do we need to start to use

EXPORT_SYMBOL_FOR_MODULES(tdx_get_max_quote_size, "tdx-guest") ?

Quoted 29 lines
arch/x86/coco/tdx/tdx.c +1 -0
+
  static void __noreturn tdx_panic(const char *msg)
  {
  	struct tdx_module_args args = {
arch/x86/include/asm/shared/tdx.h +1 -0
index 049638e3da74..2880f493a8e5 100644
@@ -49,6 +49,7 @@
  /* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
  #define TDCS_CONFIG_FLAGS		0x1110000300000016
  #define TDCS_TD_CTLS			0x1110000300000017
+#define TDCS_QUOTE_MAX_SIZE		0x9010000200000008
  #define TDCS_NOTIFY_ENABLES		0x9100000000000010
  #define TDCS_TOPOLOGY_ENUM_CONFIGURED	0x9100000000000019
 
arch/x86/include/asm/tdx.h +2 -0
index a149740b24e8..ac39674c9479 100644
@@ -72,6 +72,8 @@ int tdx_mcall_extend_rtmr(u8 index, u8 *data);
 
  u64 tdx_hcall_get_quote(u8 *buf, size_t size);
 
+u32 tdx_get_max_quote_size(void);
+
  void __init tdx_dump_attributes(u64 td_attr);
  void __init tdx_dump_td_ctls(u64 td_ctls);
 
author
#3 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Kiryl Shutsemau
Quoted 7 lines · Kiryl Shutsemau
Quoted 3 lines
Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

These supposes to be on the same line, no?

Documentation/process/coding-assistants.rst: Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]

I see... I actually used two different agents, so looks like they should be on separate lines instead?

One example that I found:
91e901c65b4d ("um: drivers: call kernel_strrchr() explicitly in cow_user.c")

[ ... ]
Assisted-by Claude:claude-opus-4-6
Assisted-by Codex:gpt-5-4
Quoted 6 lines
Quoted 2 lines

One nit below, otherwise:

Thanks for the review Kiryl!

Quoted 11 lines

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 215

Quoted 8 lines
arch/x86/coco/tdx/tdx.c +7 -0
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;

Cast is redundant.

I'll fix that, thanks.

Quoted 9 lines
Quoted 6 lines
arch/x86/coco/tdx/tdx.c +3 -0
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);
+
 static void __noreturn tdx_panic(const char *msg)
 {
 	struct tdx_module_args args = {
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author
#7 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Xiaoyao Li
Quoted 6 lines · Xiaoyao Li
Quoted 4 lines
Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

Thanks for the review Xiaoyao!

Quoted 15 lines

I have another nit other than Kiryl's

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 217

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +9 -0
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);

Do we need to start to use

Quoted 2 lines

EXPORT_SYMBOL_FOR_MODULES(tdx_get_max_quote_size, "tdx-guest") ?

This makes sense. But can we use a follow-up patch to improve this file later? Right now there are only EXPORT_SYMBOL_GPL() usages, so using EXPORT_SYMBOL_FOR_MODULES() here might look inconsistent.

author
#16 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Kiryl Shutsemau
Quoted 10 lines · Kiryl Shutsemau

drivers/virt/coco/tdx-guest/tdx-guest.c static void tdx_mr_deinit(const struct attribute_group *mr_grp) · line 174

Quoted 6 lines · Peter Fang
drivers/virt/coco/tdx-guest/tdx-guest.c +1 -1
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))

I've got confused by this offsetof(). It is valid, but why not plain sizeof()?

I recently noticed that using sizeof() on a struct with a trailing flexible array may not be the cleanest coding style [1], so I took the chance and improved it. Looking at it again, I see that I can just use struct_size_t() and not reinvent the wheel... I'll improve this in the next revision.

Quoted 4 lines

Otherwise looks okay to me:

Thanks Kiryl!

Quoted 3 lines
Signature · 2 lines
-- 
  Kiryl Shutsemau / Kirill A. Shutemov

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

#8 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Peter Fang
Quoted 25 lines · Peter Fang
Quoted 6 lines · Xiaoyao Li
Quoted 4 lines
Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

Thanks for the review Xiaoyao!

Quoted 15 lines

I have another nit other than Kiryl's

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 217

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +9 -0
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);

Do we need to start to use

Quoted 6 lines
Quoted 2 lines

EXPORT_SYMBOL_FOR_MODULES(tdx_get_max_quote_size, "tdx-guest") ?

This makes sense. But can we use a follow-up patch to improve this file later? Right now there are only EXPORT_SYMBOL_GPL() usages, so using EXPORT_SYMBOL_FOR_MODULES() here might look inconsistent.

If maintainers is going to merge the series as-is, then fine.

If we will have a v2, I don't see why cannot update to use EXPORT_SYMBOL_FOR_MODULES().

The existing ones can be updated in a separate patch before or after.

author
#9 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

Quoted 22 lines · Xiaoyao Li
Quoted 15 lines
Quoted 15 lines

I have another nit other than Kiryl's

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 217

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +9 -0
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);

Do we need to start to use

Quoted 6 lines
Quoted 2 lines

EXPORT_SYMBOL_FOR_MODULES(tdx_get_max_quote_size, "tdx-guest") ?

This makes sense. But can we use a follow-up patch to improve this file later? Right now there are only EXPORT_SYMBOL_GPL() usages, so using EXPORT_SYMBOL_FOR_MODULES() here might look inconsistent.

Quoted 6 lines

If maintainers is going to merge the series as-is, then fine.

If we will have a v2, I don't see why cannot update to use EXPORT_SYMBOL_FOR_MODULES().

The existing ones can be updated in a separate patch before or after.

Thanks. I will respin a v2 to address some review feedback. But I'd like to hold off on this improvement unless there's a strong push to do it right now. I'm going to add this to my list of guest-side cleanup items for after this series, along with the feedback I got in [1].

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

#4 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Kiryl Shutsemau
Quoted 11 lines · Kiryl Shutsemau
Quoted 11 lines
arch/x86/coco/tdx/tdx.c +10 -0
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.
+ */
+u32 tdx_get_max_quote_size(void)
Quoted 8 lines

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 215

Quoted 6 lines
arch/x86/coco/tdx/tdx.c +6 -0
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;

Cast is redundant.

I thought in Linux kernel, the explicit cast is often preferred? E.g. as documentation of the metadata field’s width here?

Quoted 8 lines
Quoted 7 lines
arch/x86/coco/tdx/tdx.c +3 -0
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);
+
 static void __noreturn tdx_panic(const char *msg)
 {
 	struct tdx_module_args args = {
Reviewed-by on v1 2/2
#17 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Peter Fang
Quoted 6 lines · Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Nit:
I think the description here should be more accurate.
"Several megabytes" colloquially implies sizes that could easily exceed 4 MB. And it's a slight contradiction VS the 4MB limitation below.

Quoted 10 lines

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

Quoted 6 lines

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

Same issue as pointed by Kiryl in the first patch.

Quoted 1 line

Also the use of struct_size_t() instead of offsetof().

Otherwise looks okay to me:

Reviewed-by on v1 1/2
#10 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Peter Fang
Quoted 9 lines · Peter Fang

TDX attestation blob ("TD Quote") sizes can grow with newer cryptographic schemes, so guests can no longer rely on a fixed-size buffer for the Quote.

Newer TDX modules report the maximum TD Quote size via a TD-scope metadata field. Add a helper to query it instead of exposing tdg_vm_rd() directly, as it can read arbitrary metadata fields.

Thanks to Xu Yilun for suggesting this.

I'm not sure what the original suggestion was. Perhaps a link to the discussion (or Xu Yilun's email) would be helpful for context.

Quoted 5 lines
Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

---

Looks good to me.

Quoted 60 lines
 arch/x86/coco/tdx/tdx.c           | 19 +++++++++++++++++++
 arch/x86/include/asm/shared/tdx.h |  1 +
 arch/x86/include/asm/tdx.h        |  2 ++
 3 files changed, 22 insertions(+)
arch/x86/coco/tdx/tdx.c +19 -0
index 186915a17c50..88c66c46e70a 100644
@@ -197,6 +197,25 @@ u64 tdx_hcall_get_quote(u8 *buf, size_t size)
 }
 EXPORT_SYMBOL_GPL(tdx_hcall_get_quote);
 
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.
+ */
+u32 tdx_get_max_quote_size(void)
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;
+}
+EXPORT_SYMBOL_GPL(tdx_get_max_quote_size);
+
 static void __noreturn tdx_panic(const char *msg)
 {
 	struct tdx_module_args args = {
arch/x86/include/asm/shared/tdx.h +1 -0
index 049638e3da74..2880f493a8e5 100644
@@ -49,6 +49,7 @@
 /* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
 #define TDCS_CONFIG_FLAGS		0x1110000300000016
 #define TDCS_TD_CTLS			0x1110000300000017
+#define TDCS_QUOTE_MAX_SIZE		0x9010000200000008
 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
 #define TDCS_TOPOLOGY_ENUM_CONFIGURED	0x9100000000000019
 
arch/x86/include/asm/tdx.h +2 -0
index a149740b24e8..ac39674c9479 100644
@@ -72,6 +72,8 @@ int tdx_mcall_extend_rtmr(u8 index, u8 *data);
 
 u64 tdx_hcall_get_quote(u8 *buf, size_t size);
 
+u32 tdx_get_max_quote_size(void);
+
 void __init tdx_dump_attributes(u64 td_attr);
 void __init tdx_dump_td_ctls(u64 td_ctls);
 
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by on v1 2/2
#19 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Peter Fang

Hi Peter,

Quoted 16 lines · Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

Quoted 8 lines

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

---

Looks good to me.

Quoted 113 lines
 drivers/virt/coco/tdx-guest/tdx-guest.c | 52 ++++++++++++++++++-------
 1 file changed, 38 insertions(+), 14 deletions(-)
drivers/virt/coco/tdx-guest/tdx-guest.c +34 -10
index a9ecc46df187..162fb47f3fae 100644
@@ -163,7 +163,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
  * DICE-based attestation uses layered evidence that requires
  * larger Quote size (~100K).
  */
-#define GET_QUOTE_BUF_SIZE		SZ_128K
+#define GET_QUOTE_DEFAULT_BUF_SIZE	SZ_128K
 
 #define GET_QUOTE_CMD_VER		1
 
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))
 
 /* struct tdx_quote_buf: Format of Quote request buffer.
  * @version: Quote format version, filled by TD.
@@ -192,8 +192,9 @@ struct tdx_quote_buf {
 	u8 data[];
 };
 
-/* Quote data buffer */
+/* Quote data buffer and size */
 static void *quote_data;
+static size_t quote_data_size;
 
 /* Lock to streamline quote requests */
 static DEFINE_MUTEX(quote_lock);
@@ -210,9 +211,8 @@ static long tdx_get_report0(struct tdx_report_req __user *req)
 			     USER_SOCKPTR(req->tdreport));
 }
 
-static void free_quote_buf(void *buf)
+static void free_quote_buf(void *buf, size_t len)
 {
-	size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
 	unsigned int count = len >> PAGE_SHIFT;
 
 	if (set_memory_encrypted((unsigned long)buf, count)) {
@@ -223,19 +223,43 @@ static void free_quote_buf(void *buf)
 	free_pages_exact(buf, len);
 }
 
-static void *alloc_quote_buf(void)
+static size_t get_quote_buf_size(void)
 {
-	size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
-	unsigned int count = len >> PAGE_SHIFT;
+	size_t buf_sz = GET_QUOTE_DEFAULT_BUF_SIZE;
+	u32 quote_sz;
+
+	quote_sz = tdx_get_max_quote_size();
+
+	if (quote_sz)
+		/* Reported size does not include GetQuote header */
+		buf_sz = TDX_QUOTE_BUF_LEN(quote_sz);
+
+	return PAGE_ALIGN(buf_sz);
+}
+
+static void *alloc_quote_buf(size_t *buflen)
+{
+	unsigned int count;
+	size_t len;
 	void *addr;
 
+	len = get_quote_buf_size();
+
+	/*
+	 * This fails if the requested size exceeds the buddy allocator's
+	 * maximum order (order-10, 4MB).
+	 */
 	addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);
 	if (!addr)
 		return NULL;
 
+	count = len >> PAGE_SHIFT;
+
 	if (set_memory_decrypted((unsigned long)addr, count))
 		return NULL;
 
+	*buflen = len;
+
 	return addr;
 }
 
@@ -286,7 +310,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (desc->inblob_len != TDX_REPORTDATA_LEN)
 		return -EINVAL;
 
-	memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
+	memset(quote_data, 0, quote_data_size);
 
 	/* Update Quote buffer header */
 	quote_buf->version = GET_QUOTE_CMD_VER;
@@ -297,7 +321,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (ret)
 		return ret;
 
-	err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
+	err = tdx_hcall_get_quote(quote_data, quote_data_size);
 	if (err) {
 		pr_err("GetQuote hypercall failed, status:%llx\n", err);
 		return -EIO;

drivers/virt/coco/tdx-guest/tdx-guest.c static int tdx_report_new_locked(struct tsm_report *report, void *data) · line 344

Quoted 6 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +1 -1
@@ -316,7 +340,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 
 	out_len = READ_ONCE(quote_buf->out_len);
 
-	if (out_len > TDX_QUOTE_MAX_LEN)
+	if (TDX_QUOTE_BUF_LEN(out_len) > quote_data_size)
 		return -EFBIG;

Nit: I think this check will be more readable if you can rename quote_data_size to quote_buf_size (since it holds total buffer size).

Quoted 29 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +3 -3
 	buf = kvmemdup(quote_buf->data, out_len, GFP_KERNEL);
@@ -418,7 +442,7 @@ static int __init tdx_guest_init(void)
 	if (ret)
 		goto deinit_mr;
 
-	quote_data = alloc_quote_buf();
+	quote_data = alloc_quote_buf(&quote_data_size);
 	if (!quote_data) {
 		pr_err("Failed to allocate Quote buffer\n");
 		ret = -ENOMEM;
@@ -432,7 +456,7 @@ static int __init tdx_guest_init(void)
 	return 0;
 
 free_quote:
-	free_quote_buf(quote_data);
+	free_quote_buf(quote_data, quote_data_size);
 free_misc:
 	misc_deregister(&tdx_misc_dev);
 deinit_mr:
@@ -445,7 +469,7 @@ module_init(tdx_guest_init);
 static void __exit tdx_guest_exit(void)
 {
 	tsm_report_unregister(&tdx_tsm_ops);
-	free_quote_buf(quote_data);
+	free_quote_buf(quote_data, quote_data_size);
 	misc_deregister(&tdx_misc_dev);
 	tdx_mr_deinit(tdx_attr_groups[0]);
 }
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
author
#5 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Binbin Wu
Quoted 21 lines · Binbin Wu
Quoted 11 lines · Kiryl Shutsemau
Quoted 11 lines
arch/x86/coco/tdx/tdx.c +10 -0
+/**
+ * tdx_get_max_quote_size() - Get the maximum TD Quote size
+ *
+ * Read the maximum size of a TD Quote from a 4-byte TD metadata field. The TDX
+ * guest driver uses it to size the buffer for Quote retrieval. Older TDX
+ * modules do not support this field and return an error.
+ *
+ * Return: Maximum Quote size in bytes on success, or 0 on failure.
+ */
+u32 tdx_get_max_quote_size(void)
Quoted 8 lines

arch/x86/coco/tdx/tdx.c u64 tdx_hcall_get_quote(u8 *buf, size_t size) · line 215

Quoted 6 lines
arch/x86/coco/tdx/tdx.c +6 -0
+{
+	u64 val, ret;
+
+	ret = tdg_vm_rd(TDCS_QUOTE_MAX_SIZE, &val);
+
+	return ret ? 0 : (u32)val;

Cast is redundant.

Quoted 2 lines

I thought in Linux kernel, the explicit cast is often preferred? E.g. as documentation of the metadata field’s width here?

Hm... I asked AI to find some patterns in arch/x86, and looks like the explicit cast is more common... Kiryl are you okay with me keeping the cast?

Quoted 1 line
author
#11 lore raw

Re: [PATCH 1/2] x86/tdx: Add helper to query maximum TD Quote size

In reply to Kuppuswamy Sathyanarayanan
Quoted 7 lines · Kuppuswamy Sathyanarayanan
Quoted 4 lines · Peter Fang

TDX attestation blob ("TD Quote") sizes can grow with newer cryptographic schemes, so guests can no longer rely on a fixed-size buffer for the Quote.

Quoted 8 lines
Quoted 5 lines

Newer TDX modules report the maximum TD Quote size via a TD-scope metadata field. Add a helper to query it instead of exposing tdg_vm_rd() directly, as it can read arbitrary metadata fields.

Thanks to Xu Yilun for suggesting this.

I'm not sure what the original suggestion was. Perhaps a link to the discussion (or Xu Yilun's email) would be helpful for context.

It was an internal discussion. I can add that to the changelog.

Quoted 11 lines
Quoted 6 lines
Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

---

Looks good to me.

Thanks Sathya!

author
#18 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Binbin Wu
Quoted 8 lines · Binbin Wu
Quoted 6 lines · Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Quoted 4 lines

Nit:
I think the description here should be more accurate.
"Several megabytes" colloquially implies sizes that could easily exceed 4 MB. And it's a slight contradiction VS the 4MB limitation below.

Good point. I'll make this clearer.

Quoted 11 lines
Quoted 11 lines

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

Quoted 8 lines
Quoted 6 lines

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

Same issue as pointed by Kiryl in the first patch.

There have been discussions about this recently [1]. I think perhaps I should drop the "Assisted-by" tags since I did not use AI to write the actual code.

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

Quoted 4 lines
Quoted 2 lines

Also the use of struct_size_t() instead of offsetof().

Yep.

Quoted 4 lines

Otherwise looks okay to me:

Thanks for the review Binbin!

author
#20 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to Kuppuswamy Sathyanarayanan
Quoted 28 lines · Kuppuswamy Sathyanarayanan

Hi Peter,

Quoted 16 lines · Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

Quoted 8 lines

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

---

Quoted 3 lines

Looks good to me.

Thanks Sathya!

Quoted 36 lines
Quoted 36 lines

 drivers/virt/coco/tdx-guest/tdx-guest.c | 52 ++++++++++++++++++-------
 1 file changed, 38 insertions(+), 14 deletions(-)
drivers/virt/coco/tdx-guest/tdx-guest.c +4 -3
index a9ecc46df187..162fb47f3fae 100644
@@ -163,7 +163,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
  * DICE-based attestation uses layered evidence that requires
  * larger Quote size (~100K).
  */
-#define GET_QUOTE_BUF_SIZE		SZ_128K
+#define GET_QUOTE_DEFAULT_BUF_SIZE	SZ_128K
 
 #define GET_QUOTE_CMD_VER		1
 
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))
 
 /* struct tdx_quote_buf: Format of Quote request buffer.
  * @version: Quote format version, filled by TD.
@@ -192,8 +192,9 @@ struct tdx_quote_buf {
 	u8 data[];
 };
 
-/* Quote data buffer */
+/* Quote data buffer and size */
 static void *quote_data;
+static size_t quote_data_size;
 

[ ... ]

Quoted 23 lines
Quoted 23 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +2 -2
@@ -286,7 +310,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (desc->inblob_len != TDX_REPORTDATA_LEN)
 		return -EINVAL;
 
-	memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
+	memset(quote_data, 0, quote_data_size);
 
 	/* Update Quote buffer header */
 	quote_buf->version = GET_QUOTE_CMD_VER;
@@ -297,7 +321,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (ret)
 		return ret;
 
-	err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
+	err = tdx_hcall_get_quote(quote_data, quote_data_size);
 	if (err) {
 		pr_err("GetQuote hypercall failed, status:%llx\n", err);
 		return -EIO;
@@ -316,7 +340,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 
 	out_len = READ_ONCE(quote_buf->out_len);
 
Quoted 7 lines

drivers/virt/coco/tdx-guest/tdx-guest.c static int tdx_report_new_locked(struct tsm_report *report, void *data) · line 344

Quoted 3 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +1 -1
-	if (out_len > TDX_QUOTE_MAX_LEN)
+	if (TDX_QUOTE_BUF_LEN(out_len) > quote_data_size)
 		return -EFBIG;

Nit: I think this check will be more readable if you can rename quote_data_size to quote_buf_size (since it holds total buffer size).

Hm. This pairs with the original "static void *quote_data". Or perhaps "quote_data_len"?

Quoted 1 line
#21 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

Quoted 105 lines · Peter Fang
Quoted 28 lines · Kuppuswamy Sathyanarayanan

Hi Peter,

Quoted 16 lines · Peter Fang

From: Kuppuswamy Sathyanarayanan <[email protected]>

The TDX attestation driver currently uses a fixed 128 KB Quote buffer shared with the host VMM. This may be too small for Quotes using schemes such as post-quantum cryptography (PQC), where certificate chains can increase the Quote size to several megabytes.

Allocate the Quote buffer based on the size reported by the TDX module instead of always reserving a fixed-size buffer. This avoids wasting memory on platforms that do not require larger Quotes. Older platforms fall back to the default 128 KB buffer.

Because the Quote buffer must be physically contiguous, its size is bound by the buddy allocator's maximum page order (4 MB), which should be sufficient for current attestation needs.

Quoted 8 lines

struct tdx_quote_buf has a trailing flexible array, so use offsetof() instead of sizeof() to calculate the header size.

Assisted-by Claude:claude-opus-4-7
Assisted-by GitHub Copilot:gpt-5.4

---

Quoted 3 lines

Looks good to me.

Thanks Sathya!

Quoted 36 lines
Quoted 36 lines

 drivers/virt/coco/tdx-guest/tdx-guest.c | 52 ++++++++++++++++++-------
 1 file changed, 38 insertions(+), 14 deletions(-)
drivers/virt/coco/tdx-guest/tdx-guest.c +4 -3
index a9ecc46df187..162fb47f3fae 100644
@@ -163,7 +163,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
  * DICE-based attestation uses layered evidence that requires
  * larger Quote size (~100K).
  */
-#define GET_QUOTE_BUF_SIZE		SZ_128K
+#define GET_QUOTE_DEFAULT_BUF_SIZE	SZ_128K
 
 #define GET_QUOTE_CMD_VER		1
 
@@ -171,7 +171,7 @@ static void tdx_mr_deinit(const struct attribute_group *mr_grp)
 #define GET_QUOTE_SUCCESS		0
 #define GET_QUOTE_IN_FLIGHT		0xffffffffffffffff
 
-#define TDX_QUOTE_MAX_LEN		(GET_QUOTE_BUF_SIZE - sizeof(struct tdx_quote_buf))
+#define TDX_QUOTE_BUF_LEN(n)		(offsetof(struct tdx_quote_buf, data) + (n))
 
 /* struct tdx_quote_buf: Format of Quote request buffer.
  * @version: Quote format version, filled by TD.
@@ -192,8 +192,9 @@ struct tdx_quote_buf {
 	u8 data[];
 };
 
-/* Quote data buffer */
+/* Quote data buffer and size */
 static void *quote_data;
+static size_t quote_data_size;
 

[ ... ]

Quoted 23 lines
Quoted 23 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +2 -2
@@ -286,7 +310,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (desc->inblob_len != TDX_REPORTDATA_LEN)
 		return -EINVAL;
 
-	memset(quote_data, 0, GET_QUOTE_BUF_SIZE);
+	memset(quote_data, 0, quote_data_size);
 
 	/* Update Quote buffer header */
 	quote_buf->version = GET_QUOTE_CMD_VER;
@@ -297,7 +321,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 	if (ret)
 		return ret;
 
-	err = tdx_hcall_get_quote(quote_data, GET_QUOTE_BUF_SIZE);
+	err = tdx_hcall_get_quote(quote_data, quote_data_size);
 	if (err) {
 		pr_err("GetQuote hypercall failed, status:%llx\n", err);
 		return -EIO;
@@ -316,7 +340,7 @@ static int tdx_report_new_locked(struct tsm_report *report, void *data)
 
 	out_len = READ_ONCE(quote_buf->out_len);
 
Quoted 7 lines

drivers/virt/coco/tdx-guest/tdx-guest.c static int tdx_report_new_locked(struct tsm_report *report, void *data) · line 344

Quoted 3 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +1 -1
-	if (out_len > TDX_QUOTE_MAX_LEN)
+	if (TDX_QUOTE_BUF_LEN(out_len) > quote_data_size)
 		return -EFBIG;

Nit: I think this check will be more readable if you can rename quote_data_size to quote_buf_size (since it holds total buffer size).

Quoted 2 lines

Hm. This pairs with the original "static void *quote_data". Or perhaps "quote_data_len"?

Yes, it pairs with quote_data buffer name. May be both should be renamed.

If it is too much trouble, just leave it.

Quoted 2 lines
Quoted 2 lines
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
author
#14 lore raw

Re: [PATCH 2/2] virt: tdx-guest: Allocate Quote buffer dynamically

In reply to [email protected]
Quoted 38 lines · On Fri, Jun 12, 2026 at 11:22:02AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unbounded host-provided quote size causes guest kernel warning (and potential panic) during allocation. --

commit 918e9fdc48e5a03b41dab3a999be1a3f631327c1
Author: Kuppuswamy Sathyanarayanan <[email protected]>

virt: tdx-guest: Allocate Quote buffer dynamically

This commit modifies the TDX attestation driver to allocate the Quote buffer dynamically based on the size reported by the TDX module. It falls back to a default 128 KB buffer if the reported size is unavailable, supporting larger quotes needed for newer schemes.

Quoted 4 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +4 -0
+
+static void *alloc_quote_buf(size_t *buflen)
+{
+	unsigned int count;

drivers/virt/coco/tdx-guest/tdx-guest.c static void free_quote_buf(void *buf) · line 252

Quoted 10 lines
drivers/virt/coco/tdx-guest/tdx-guest.c +7 -0
+	size_t len;
 	void *addr;
 
+	len = get_quote_buf_size();
+
+	/*
+	 * This fails if the requested size exceeds the buddy allocator's
+	 * maximum order (order-10, 4MB).
+	 */
 	addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);

[Severity: High]
If the host provides an excessively large quote_sz, could this allocation trigger a kernel warning?

The comments indicate the buddy allocator will fail requests exceeding its maximum order. However, passing an unbounded, host-controlled size directly to alloc_pages_exact() without __GFP_NOWARN will trigger a WARN_ON_ONCE_GFP in the page allocator's slow path before failing.

Quoted 6 lines

If a host is malicious or misconfigured, could this be used to spam the guest's kernel log or cause a denial-of-service crash if the guest runs with panic_on_warn=1?

Would it be better to explicitly check the size against a reasonable upper bound or include the __GFP_NOWARN flag here?

Good point. I'll add __GFP_NOWARN here since the host is untrusted.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2