Skip to content

Commit 48547fe

Browse files
committed
KVM: SVM: Add a helper to convert a SME-aware PA back to a struct page
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>
1 parent c501062 commit 48547fe

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,7 @@ static void svm_hardware_unsetup(void)
11241124
for_each_possible_cpu(cpu)
11251125
svm_cpu_uninit(cpu);
11261126

1127-
__free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
1128-
get_order(IOPM_SIZE));
1127+
__free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE));
11291128
iopm_base = 0;
11301129
}
11311130

@@ -1301,7 +1300,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
13011300
if (!kvm_hlt_in_guest(vcpu->kvm))
13021301
svm_set_intercept(svm, INTERCEPT_HLT);
13031302

1304-
control->iopm_base_pa = __sme_set(iopm_base);
1303+
control->iopm_base_pa = iopm_base;
13051304
control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
13061305
control->int_ctl = V_INTR_MASKING_MASK;
13071306

@@ -1503,7 +1502,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
15031502

15041503
sev_free_vcpu(vcpu);
15051504

1506-
__free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
1505+
__free_page(__sme_pa_to_page(svm->vmcb01.pa));
15071506
__free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
15081507
}
15091508

@@ -5251,7 +5250,7 @@ static __init int svm_hardware_setup(void)
52515250

52525251
iopm_va = page_address(iopm_pages);
52535252
memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
5254-
iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
5253+
iopm_base = __sme_page_pa(iopm_pages);
52555254

52565255
init_msrpm_offsets();
52575256

arch/x86/kvm/svm/svm.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@
2525
#include "cpuid.h"
2626
#include "kvm_cache_regs.h"
2727

28-
#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
28+
/*
29+
* Helpers to convert to/from physical addresses for pages whose address is
30+
* consumed directly by hardware. Even though it's a physical address, SVM
31+
* often restricts the address to the natural width, hence 'unsigned long'
32+
* instead of 'hpa_t'.
33+
*/
34+
static inline unsigned long __sme_page_pa(struct page *page)
35+
{
36+
return __sme_set(page_to_pfn(page) << PAGE_SHIFT);
37+
}
38+
39+
static inline struct page *__sme_pa_to_page(unsigned long pa)
40+
{
41+
return pfn_to_page(__sme_clr(pa) >> PAGE_SHIFT);
42+
}
2943

3044
#define IOPM_SIZE PAGE_SIZE * 3
3145
#define MSRPM_SIZE PAGE_SIZE * 2

0 commit comments

Comments
 (0)