Re: [PATCH v11 12/20] x86/virt/tdx: Allocate and set up PAMTs for TDMRs

From: Huang, Kai
Date: Sun Jun 11 2023 - 22:52:26 EST


On Thu, 2023-06-08 at 16:43 -0700, Dave Hansen wrote:
> On 6/8/23 16:24, kirill.shutemov@xxxxxxxxxxxxxxx wrote:
> > > ret = -EINVAL;
> > > + if (ret)
> > > + tdmrs_free_pamt_all(&tdx_tdmr_list);
> > > + else
> > > + pr_info("%lu KBs allocated for PAMT.\n",
> > > + tdmrs_count_pamt_pages(&tdx_tdmr_list) * 4);
> > "* 4"? This is very cryptic. procfs uses "<< (PAGE_SHIFT - 10)" which
> > slightly less magic to me. And just make the helper that returns kilobytes
> > to begin with, if it is the only caller.
>
> Let's look at where this data comes from:
>
> +static unsigned long tdmrs_count_pamt_pages(struct tdmr_info_list
> *tdmr_list)
> +{
> + unsigned long pamt_npages = 0;
> + int i;
> +
> + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) {
> + unsigned long pfn, npages;
> +
> + tdmr_get_pamt(tdmr_entry(tdmr_list, i), &pfn, &npages);
> + pamt_npages += npages;
> + }
>
> OK, so tdmr_get_pamt() is getting it in pages. How is it *stored*?
>
> +static void tdmr_get_pamt(struct tdmr_info *tdmr, unsigned long *pamt_pfn,
> + unsigned long *pamt_npages)
> +{
> ...
> + pamt_sz = tdmr->pamt_4k_size + tdmr->pamt_2m_size + tdmr->pamt_1g_size;
> ++ *pamt_pfn = PHYS_PFN(pamt_base);
> + *pamt_npages = pamt_sz >> PAGE_SHIFT;
> +}
>
> Oh, it's actually stored in bytes. So to print it out you actually
> convert it from bytes->pages->kbytes. Not the best.
>
> If tdmr_get_pamt() just returned 'pamt_size_bytes', you could do one
> conversion at:
>
> free_contig_range(pamt_pfn, pamt_size_bytes >> PAGE_SIZE);

I thought making tdmr_get_pamt() return pamt_pfn and pamt_npages would be more
clear that PAMTs must be in 4K granularity, but I guess it doesn't matter
anyway.

If we return bytes for PAMT size, I think we should also return physical address
instead of PFN for PAMT start?

I'll change tdmr_get_pamt() to return physical address and bytes for PAMT
location and size respectively. Please let me know if you have any comments.

>
> and since tdmrs_count_pamt_pages() has only one caller you can just make
> it: tdmrs_count_pamt_kb(). The print becomes:
>
> pr_info("%lu KBs allocated for PAMT.\n",
> tdmrs_count_pamt_kb(&tdx_tdmr_list) * 4);
>
> and tdmrs_count_pamt_kb() does something super fancy like:
>
> return pamt_size_bytes / 1024;
>
> which makes total complete obvious sense and needs zero explanation.

Will do.

Thanks for the feedback.