Drivers: hv: Don't remap addresses that are above shared_gpa_boundary
authorMichael Kelley <mikelley@microsoft.com>
Sun, 26 Mar 2023 13:52:06 +0000 (06:52 -0700)
committerWei Liu <wei.liu@kernel.org>
Mon, 17 Apr 2023 19:19:04 +0000 (19:19 +0000)
With the vTOM bit now treated as a protection flag and not part of
the physical address, avoid remapping physical addresses with vTOM set
since technically such addresses aren't valid.  Use ioremap_cache()
instead of memremap() to ensure that the mapping provides decrypted
access, which will correctly set the vTOM bit as a protection flag.

While this change is not required for correctness with the current
implementation of memremap(), for general code hygiene it's better to
not depend on the mapping functions doing something reasonable with
a physical address that is out-of-range.

While here, fix typos in two error messages.

Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Reviewed-by: Tianyu Lan <Tianyu.Lan@microsoft.com>
Link: https://lore.kernel.org/r/1679838727-87310-12-git-send-email-mikelley@microsoft.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
arch/x86/hyperv/hv_init.c
drivers/hv/hv.c

index edbc67ec1f3ecf91a0d484bd490a2cf0055f1da6..a5f9474f08e123fa6c703e29ecfe2049763fd2cc 100644 (file)
@@ -63,7 +63,10 @@ static int hyperv_init_ghcb(void)
         * memory boundary and map it here.
         */
        rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
-       ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
+
+       /* Mask out vTOM bit. ioremap_cache() maps decrypted */
+       ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
+       ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
        if (!ghcb_va)
                return -ENOMEM;
 
@@ -217,7 +220,7 @@ static int hv_cpu_die(unsigned int cpu)
        if (hv_ghcb_pg) {
                ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
                if (*ghcb_va)
-                       memunmap(*ghcb_va);
+                       iounmap(*ghcb_va);
                *ghcb_va = NULL;
        }
 
index 8b0dd8e5244d70b50da53733b68b27a876535aa3..008234894d2878ab13d5a42cd6e868d55f2641dd 100644 (file)
@@ -217,11 +217,13 @@ void hv_synic_enable_regs(unsigned int cpu)
        simp.simp_enabled = 1;
 
        if (hv_isolation_type_snp() || hv_root_partition) {
+               /* Mask out vTOM bit. ioremap_cache() maps decrypted */
+               u64 base = (simp.base_simp_gpa << HV_HYP_PAGE_SHIFT) &
+                               ~ms_hyperv.shared_gpa_boundary;
                hv_cpu->synic_message_page
-                       = memremap(simp.base_simp_gpa << HV_HYP_PAGE_SHIFT,
-                                  HV_HYP_PAGE_SIZE, MEMREMAP_WB);
+                       = (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
                if (!hv_cpu->synic_message_page)
-                       pr_err("Fail to map syinc message page.\n");
+                       pr_err("Fail to map synic message page.\n");
        } else {
                simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
                        >> HV_HYP_PAGE_SHIFT;
@@ -234,12 +236,13 @@ void hv_synic_enable_regs(unsigned int cpu)
        siefp.siefp_enabled = 1;
 
        if (hv_isolation_type_snp() || hv_root_partition) {
-               hv_cpu->synic_event_page =
-                       memremap(siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT,
-                                HV_HYP_PAGE_SIZE, MEMREMAP_WB);
-
+               /* Mask out vTOM bit. ioremap_cache() maps decrypted */
+               u64 base = (siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT) &
+                               ~ms_hyperv.shared_gpa_boundary;
+               hv_cpu->synic_event_page
+                       = (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
                if (!hv_cpu->synic_event_page)
-                       pr_err("Fail to map syinc event page.\n");
+                       pr_err("Fail to map synic event page.\n");
        } else {
                siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
                        >> HV_HYP_PAGE_SHIFT;
@@ -316,7 +319,7 @@ void hv_synic_disable_regs(unsigned int cpu)
         */
        simp.simp_enabled = 0;
        if (hv_isolation_type_snp() || hv_root_partition) {
-               memunmap(hv_cpu->synic_message_page);
+               iounmap(hv_cpu->synic_message_page);
                hv_cpu->synic_message_page = NULL;
        } else {
                simp.base_simp_gpa = 0;
@@ -328,7 +331,7 @@ void hv_synic_disable_regs(unsigned int cpu)
        siefp.siefp_enabled = 0;
 
        if (hv_isolation_type_snp() || hv_root_partition) {
-               memunmap(hv_cpu->synic_event_page);
+               iounmap(hv_cpu->synic_event_page);
                hv_cpu->synic_event_page = NULL;
        } else {
                siefp.base_siefp_gpa = 0;