Re: [PATCH v3 06/16] x86/virt/tdx: Improve PAMT refcounters allocation for sparse memory

Huang, Kai
2025-10-15T01:35:24Z

> +/*
> + * Allocate PAMT reference counters for the given PFN range.
> + *
> + * It consumes 2MiB for every 1TiB of physical memory.
> + */
> +static int alloc_pamt_refcount(unsigned long start_pfn, unsigned long end_pfn)
> +{
> + unsigned long start, end;
> +
> + start = (unsigned long)tdx_find_pamt_refcount(PFN_PHYS(start_pfn));
> + end = (unsigned long)tdx_find_pamt_refcount(PFN_PHYS(end_pfn + 1));
> + start = round_down(start, PAGE_SIZE);
> + end = round_up(end, PAGE_SIZE);
> +
> + return apply_to_page_range(&init_mm, start, end - start,
> + pamt_refcount_populate, NULL);
> +}
>

This alloc_pamt_refcount() needs to check whether DPAMT is supported
before calling apply_to_page_range(). Otherwise when DPAMT is not
supported the apply_to_page_range() panics due to the @pamt_refcounts is
never initialized to a proper value.

> @@ -288,10 +377,19 @@ static int build_tdx_memlist(struct list_head *tmb_list)
> ret = add_tdx_memblock(tmb_list, start_pfn, end_pfn, nid);
> if (ret)
> goto err;
> +
> + /* Allocated PAMT refcountes for the memblock */
> + ret = alloc_pamt_refcount(start_pfn, end_pfn);
> + if (ret)
> + goto err;
> }