]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: SVM: Add a helper to convert a SME-aware PA back to a struct page
authorSean Christopherson <seanjc@google.com>
Fri, 2 Aug 2024 20:45:09 +0000 (13:45 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 30 Aug 2024 02:06:12 +0000 (19:06 -0700)
Add __sme_pa_to_page() to pair with __sme_page_pa() and use it to replace
open coded equivalents, including for "iopm_base", which previously
avoided having to do __sme_clr() by storing the raw PA in the global
variable.

Opportunistically convert __sme_page_pa() to a helper to provide type
safety.

No functional change intended.

Link: https://lore.kernel.org/r/20240802204511.352017-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index d6f252555ab3ff0c68ead0ab03013ec8782364ab..dd1cfee3e38fc7ac733cce37a5d92ba25f7e1741 100644 (file)
@@ -1124,8 +1124,7 @@ static void svm_hardware_unsetup(void)
        for_each_possible_cpu(cpu)
                svm_cpu_uninit(cpu);
 
-       __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
-       get_order(IOPM_SIZE));
+       __free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE));
        iopm_base = 0;
 }
 
@@ -1301,7 +1300,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
        if (!kvm_hlt_in_guest(vcpu->kvm))
                svm_set_intercept(svm, INTERCEPT_HLT);
 
-       control->iopm_base_pa = __sme_set(iopm_base);
+       control->iopm_base_pa = iopm_base;
        control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
        control->int_ctl = V_INTR_MASKING_MASK;
 
@@ -1503,7 +1502,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
 
        sev_free_vcpu(vcpu);
 
-       __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
+       __free_page(__sme_pa_to_page(svm->vmcb01.pa));
        __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
 }
 
@@ -5251,7 +5250,7 @@ static __init int svm_hardware_setup(void)
 
        iopm_va = page_address(iopm_pages);
        memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
-       iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
+       iopm_base = __sme_page_pa(iopm_pages);
 
        init_msrpm_offsets();
 
index 76107c7d0595d790457209f120d420e99dfc5e15..2b095acdb97f1f807569de59895a0f24823af0a6 100644 (file)
 #include "cpuid.h"
 #include "kvm_cache_regs.h"
 
-#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
+/*
+ * Helpers to convert to/from physical addresses for pages whose address is
+ * consumed directly by hardware.  Even though it's a physical address, SVM
+ * often restricts the address to the natural width, hence 'unsigned long'
+ * instead of 'hpa_t'.
+ */
+static inline unsigned long __sme_page_pa(struct page *page)
+{
+       return __sme_set(page_to_pfn(page) << PAGE_SHIFT);
+}
+
+static inline struct page *__sme_pa_to_page(unsigned long pa)
+{
+       return pfn_to_page(__sme_clr(pa) >> PAGE_SHIFT);
+}
 
 #define        IOPM_SIZE PAGE_SIZE * 3
 #define        MSRPM_SIZE PAGE_SIZE * 2