Re: [PATCH RESEND v9 2/2] x86/tdx: Support vmalloc() for tdx_enc_status_changed()

From: Dave Hansen
Date: Fri Aug 11 2023 - 10:09:45 EST


On 8/10/23 19:12, Dexuan Cui wrote:
> + if (!is_vmalloc_addr((void *)start))
> + return tdx_enc_status_changed_phys(__pa(start), __pa(end), enc);
> +
> + while (start < end) {
> + phys_addr_t start_pa = slow_virt_to_phys((void *)start);
> + phys_addr_t end_pa = start_pa + PAGE_SIZE;
> +
> + if (!tdx_enc_status_changed_phys(start_pa, end_pa, enc))
> + return false;
> +
> + start += PAGE_SIZE;
> + }

This creates two different paths for vmalloc() and the direct map.
There are two different ways to do va=>pa conversion, for instance.
Here's a single loop that works for both cases:

unsigned long step = end - start;
unsigned long addr;

/* Step through page-by-page for vmalloc() mappings: */
if (is_vmalloc_addr((void *)vaddr))
step = PAGE_SIZE;

for (addr = start; addr < end; addr += step) {
phys_addr_t start_pa = slow_virt_to_phys(addr);
phys_addr_t end_pa = start_pa + step;

if (!tdx_enc_status_changed_phys(start_pa, end_pa, enc))
return false;
}

Note that this also doesn't abuse 'start' by making it a loop variable.
It also, uh, uses a for() loop.

The only downside is that it costs a page table walk for direct map
virt=>phys conversion. I can live with that.