KVM: x86/mmu: Factor out allocating memslot rmap
Small refactor to facilitate allocating rmaps for all memslots at once. No functional change expected. Signed-off-by: Ben Gardon <bgardon@google.com> Message-Id: <20210518173414.450044-3-bgardon@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
c9b929b3fa
commit
56dd1019c8
@ -10941,10 +10941,31 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
|
|||||||
kvm_page_track_free_memslot(slot);
|
kvm_page_track_free_memslot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int memslot_rmap_alloc(struct kvm_memory_slot *slot,
|
||||||
|
unsigned long npages)
|
||||||
|
{
|
||||||
|
const int sz = sizeof(*slot->arch.rmap[0]);
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
|
||||||
|
int level = i + 1;
|
||||||
|
int lpages = gfn_to_index(slot->base_gfn + npages - 1,
|
||||||
|
slot->base_gfn, level) + 1;
|
||||||
|
|
||||||
|
slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
|
||||||
|
if (!slot->arch.rmap[i]) {
|
||||||
|
memslot_rmap_free(slot);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
|
static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
|
||||||
unsigned long npages)
|
unsigned long npages)
|
||||||
{
|
{
|
||||||
int i;
|
int i, r;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Clear out the previous array pointers for the KVM_MR_MOVE case. The
|
* Clear out the previous array pointers for the KVM_MR_MOVE case. The
|
||||||
@ -10953,7 +10974,11 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
|
|||||||
*/
|
*/
|
||||||
memset(&slot->arch, 0, sizeof(slot->arch));
|
memset(&slot->arch, 0, sizeof(slot->arch));
|
||||||
|
|
||||||
for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
|
r = memslot_rmap_alloc(slot, npages);
|
||||||
|
if (r)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) {
|
||||||
struct kvm_lpage_info *linfo;
|
struct kvm_lpage_info *linfo;
|
||||||
unsigned long ugfn;
|
unsigned long ugfn;
|
||||||
int lpages;
|
int lpages;
|
||||||
@ -10962,14 +10987,6 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot,
|
|||||||
lpages = gfn_to_index(slot->base_gfn + npages - 1,
|
lpages = gfn_to_index(slot->base_gfn + npages - 1,
|
||||||
slot->base_gfn, level) + 1;
|
slot->base_gfn, level) + 1;
|
||||||
|
|
||||||
slot->arch.rmap[i] =
|
|
||||||
kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
|
|
||||||
GFP_KERNEL_ACCOUNT);
|
|
||||||
if (!slot->arch.rmap[i])
|
|
||||||
goto out_free;
|
|
||||||
if (i == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
|
linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
|
||||||
if (!linfo)
|
if (!linfo)
|
||||||
goto out_free;
|
goto out_free;
|
||||||
|
Loading…
Reference in New Issue
Block a user