Skip to content

Commit 73be81b

Browse files
committed
KVM: SVM: Add a helper to allocate and initialize permissions bitmaps
Add a helper to allocate and initialize an MSR or I/O permissions map, as the logic is identical between the two map types, the only difference is the size of the bitmap. Opportunistically add a comment to explain why the bitmaps are initialized with 0xff, e.g. instead of the more common zero-initialized behavior, which is the main motivation for deduplicating the code. No functional change intended. Link: https://lore.kernel.org/r/20250610225737.156318-31-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 54f1c77 commit 73be81b

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -722,19 +722,23 @@ void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
722722
svm->nested.force_msr_bitmap_recalc = true;
723723
}
724724

725-
void *svm_vcpu_alloc_msrpm(void)
725+
void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)
726726
{
727-
unsigned int order = get_order(MSRPM_SIZE);
728-
struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
729-
void *msrpm;
727+
unsigned int order = get_order(size);
728+
struct page *pages = alloc_pages(gfp_mask, order);
729+
void *pm;
730730

731731
if (!pages)
732732
return NULL;
733733

734-
msrpm = page_address(pages);
735-
memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
734+
/*
735+
* Set all bits in the permissions map so that all MSR and I/O accesses
736+
* are intercepted by default.
737+
*/
738+
pm = page_address(pages);
739+
memset(pm, 0xff, PAGE_SIZE * (1 << order));
736740

737-
return msrpm;
741+
return pm;
738742
}
739743

740744
static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
@@ -5314,11 +5318,8 @@ static __init void svm_set_cpu_caps(void)
53145318

53155319
static __init int svm_hardware_setup(void)
53165320
{
5317-
int cpu;
5318-
struct page *iopm_pages;
53195321
void *iopm_va;
5320-
int r;
5321-
unsigned int order = get_order(IOPM_SIZE);
5322+
int cpu, r;
53225323

53235324
/*
53245325
* NX is required for shadow paging and for NPT if the NX huge pages
@@ -5399,13 +5400,11 @@ static __init int svm_hardware_setup(void)
53995400
pr_info("LBR virtualization supported\n");
54005401
}
54015402

5402-
iopm_pages = alloc_pages(GFP_KERNEL, order);
5403-
if (!iopm_pages)
5403+
iopm_va = svm_alloc_permissions_map(IOPM_SIZE, GFP_KERNEL);
5404+
if (!iopm_va)
54045405
return -ENOMEM;
54055406

5406-
iopm_va = page_address(iopm_pages);
5407-
memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
5408-
iopm_base = __sme_page_pa(iopm_pages);
5407+
iopm_base = __sme_set(__pa(iopm_va));
54095408

54105409
/*
54115410
* Note, SEV setup consumes npt_enabled and enable_mmio_caching (which

arch/x86/kvm/svm/svm.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,13 @@ BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set)
667667
/* svm.c */
668668
extern bool dump_invalid_vmcb;
669669

670-
void *svm_vcpu_alloc_msrpm(void);
670+
void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask);
671+
672+
static inline void *svm_vcpu_alloc_msrpm(void)
673+
{
674+
return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT);
675+
}
676+
671677
void svm_vcpu_free_msrpm(void *msrpm);
672678
void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
673679
void svm_enable_lbrv(struct kvm_vcpu *vcpu);

0 commit comments

Comments
 (0)