When a TD mode Linux TDX VM runs on Hyper-V, the Linux hv_netvsc driver needs to share a vmalloc()'d buffer with the host OS: see netvsc_init_buf() -> vmbus_establish_gpadl() -> ... -> __vmbus_establish_gpadl() -> set_memory_decrypted(). Currently set_memory_decrypted() doesn't work for a vmalloc()'d buffer because tdx_enc_status_changed() uses __pa(vaddr), i.e., it assumes that the 'vaddr' can't be from vmalloc(), and consequently hv_netvsc fails to load. Fix this by handling the pages one by one. hv_netvsc is the first user of vmalloc() + set_memory_decrypted(), which is why nobody noticed this until now. Co-developed-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx> Signed-off-by: Dexuan Cui <decui@xxxxxxxxxxxxx> Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Reviewed-by: Michael Kelley <mikelley@xxxxxxxxxxxxx> Reviewed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.i= ntel.com> Reviewed-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx> Reviewed-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Acked-by: Kai Huang <kai.huang@xxxxxxxxx> Cc: stable@xxxxxxxxxxxxxxx # 6.6+ --- arch/x86/coco/tdx/tdx.c | 43 ++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 078e2bac25531..8f471260924f7 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -8,6 +8,7 @@ #include <linux/export.h> #include <linux/io.h> #include <linux/kexec.h> +#include <linux/mm.h> #include <asm/coco.h> #include <asm/tdx.h> #include <asm/vmx.h> @@ -782,6 +783,19 @@ static bool tdx_map_gpa(phys_addr_t start, phys_addr_t= end, bool enc) return false; } =20 +static bool tdx_enc_status_changed_phys(phys_addr_t start, phys_addr_t end= , + bool enc) +{ + if (!tdx_map_gpa(start, end, enc)) + return false; + + /* shared->private conversion requires memory to be accepted before use *= / + if (enc) + return tdx_accept_memory(start, end); + + return true; +} + /* * Inform the VMM of the guest's intent for this physical page: shared wit= h * the VMM or private to the guest. The VMM is expected to change its map= ping @@ -789,15 +803,30 @@ static bool tdx_map_gpa(phys_addr_t start, phys_addr_= t end, bool enc) */ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool= enc) { - phys_addr_t start =3D __pa(vaddr); - phys_addr_t end =3D __pa(vaddr + numpages * PAGE_SIZE); + unsigned long start =3D vaddr; + unsigned long end =3D start + numpages * PAGE_SIZE; + unsigned long step =3D end - start; + unsigned long addr; + + /* Step through page-by-page for vmalloc() mappings */ + if (is_vmalloc_addr((void *)vaddr)) + step =3D PAGE_SIZE; + + for (addr =3D start; addr < end; addr +=3D step) { + phys_addr_t start_pa; + phys_addr_t end_pa; + + /* The check fails on vmalloc() mappings */ + if (virt_addr_valid(addr)) + start_pa =3D __pa(addr); + else + start_pa =3D slow_virt_to_phys((void *)addr); =20 - if (!tdx_map_gpa(start, end, enc)) - return false; + end_pa =3D start_pa + step; =20 - /* shared->private conversion requires memory to be accepted before use *= / - if (enc) - return tdx_accept_memory(start, end); + if (!tdx_enc_status_changed_phys(start_pa, end_pa, enc)) + return false; + } =20 return true; } --=20 2.25.1 --_002_SA1PR21MB1317816DFCE6EF38A92CF254BFDA2SA1PR21MB1317namp_ Content-Type: application/octet-stream; name="0001-x86-tdx-Fix-set_memory_decrypted-for-vmalloc-buffers.patch" Content-Description: 0001-x86-tdx-Fix-set_memory_decrypted-for-vmalloc-buffers.patch Content-Disposition: attachment; filename="0001-x86-tdx-Fix-set_memory_decrypted-for-vmalloc-buffers.patch"; size=3592; creation-date="Mon, 08 Jul 2024 21:42:15 GMT"; modification-date="Mon, 08 Jul 2024 21:45:23 GMT" Content-Transfer-Encoding: base64 RnJvbSAxMzJmNjU2ZmRiZjNiNGYwMDc1Mj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