Re: [PATCH v3 3/4] virt: tdx-guest: Use a variable to store the Quote buffer size
From: Peter Fang
Date: Thu Sep 03 2026 - 21:33:55 EST
On Thu, Sep 03, 2026 at 01:59:25PM +0800, Xiaoyao Li wrote:
> On 9/3/2026 7:15 AM, Peter Fang wrote:
> > You are also dropping PAGE_ALIGN() in alloc and free calls. You can
> > mention it in commit log.
>
> I think it's better to keep the PAGE_ALIGN() when assign quote_data_len,
> although GET_QUOTE_BUF_SIZE is aligned already. This makes it super clear
> we want to page size align the buffer.
>
> And in next patch, we can make it
>
> quote_data_len = PAGE_ALIGN(get_quote_buf_size());
Hmm... Looking at this again actually makes me think it could be
reworked a bit. There's a discrepancy in alloc_quote_buf() if for
whatever reason @len is not page aligned. alloc_pages_exact() rounds it
up internally but set_memory_decrypted() rounds it *down*, so we'd end
up with a useless page at the end...
What if alloc_quote_buf() took a @npages instead? Then the expectations
would be pretty clear:
-static void *alloc_quote_buf(void)
+static unsigned int get_quote_buf_npages(void)
+{
+ u32 quote_size = tdx_get_max_quote_size();
+ size_t len;
+
+ if (quote_size)
+ len = TDX_QUOTE_BUF_LEN(quote_size);
+ else
+ len = GET_QUOTE_DEFAULT_BUF_SIZE;
+
+ return PFN_UP(len);
+}
+
+static void *alloc_quote_buf(unsigned int npages)
{
- size_t len = PAGE_ALIGN(GET_QUOTE_BUF_SIZE);
- unsigned int count = len >> PAGE_SHIFT;
void *addr;
- addr = alloc_pages_exact(len, GFP_KERNEL | __GFP_ZERO);
+ addr = alloc_pages_exact(PAGE_SIZE * npages, GFP_KERNEL | __GFP_ZERO);
if (!addr)
return NULL;
- if (set_memory_decrypted((unsigned long)addr, count))
+ if (set_memory_decrypted((unsigned long)addr, npages))
return NULL;
return addr;
}
static int __init tdx_guest_init(void)
{
...
- quote_data = alloc_quote_buf();
+ quote_data_npages = get_quote_buf_npages();
+ quote_data = alloc_quote_buf(quote_data_npages);
if (!quote_data) {
...
}
}
Does this look better to you?