Skip to content

Commit db3fe4e

Browse files
Takuya Yoshikawaavikivity
authored andcommitted
KVM: Introduce kvm_memory_slot::arch and move lpage_info into it
Some members of kvm_memory_slot are not used by every architecture. This patch is the first step to make this difference clear by introducing kvm_memory_slot::arch; lpage_info is moved into it. Signed-off-by: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 189a2f7 commit db3fe4e

File tree

11 files changed

+122
-68
lines changed

11 files changed

+122
-68
lines changed

arch/ia64/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ struct kvm_sal_data {
459459
unsigned long boot_gp;
460460
};
461461

462+
struct kvm_arch_memory_slot {
463+
};
464+
462465
struct kvm_arch {
463466
spinlock_t dirty_log_lock;
464467

arch/ia64/kvm/kvm-ia64.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,16 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
15711571
return VM_FAULT_SIGBUS;
15721572
}
15731573

1574+
void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1575+
struct kvm_memory_slot *dont)
1576+
{
1577+
}
1578+
1579+
int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1580+
{
1581+
return 0;
1582+
}
1583+
15741584
int kvm_arch_prepare_memory_region(struct kvm *kvm,
15751585
struct kvm_memory_slot *memslot,
15761586
struct kvm_memory_slot old,

arch/powerpc/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ struct revmap_entry {
213213
#define KVMPPC_PAGE_WRITETHRU HPTE_R_W /* 0x40 */
214214
#define KVMPPC_GOT_PAGE 0x80
215215

216+
struct kvm_arch_memory_slot {
217+
};
218+
216219
struct kvm_arch {
217220
#ifdef CONFIG_KVM_BOOK3S_64_HV
218221
unsigned long hpt_virt;

arch/powerpc/kvm/powerpc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ long kvm_arch_dev_ioctl(struct file *filp,
281281
return -EINVAL;
282282
}
283283

284+
void kvm_arch_free_memslot(struct kvm_memory_slot *free,
285+
struct kvm_memory_slot *dont)
286+
{
287+
}
288+
289+
int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
290+
{
291+
return 0;
292+
}
293+
284294
int kvm_arch_prepare_memory_region(struct kvm *kvm,
285295
struct kvm_memory_slot *memslot,
286296
struct kvm_memory_slot old,

arch/s390/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ struct kvm_vm_stat {
245245
u32 remote_tlb_flush;
246246
};
247247

248+
struct kvm_arch_memory_slot {
249+
};
250+
248251
struct kvm_arch{
249252
struct sca_block *sca;
250253
debug_info_t *dbf;

arch/s390/kvm/kvm-s390.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,16 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
814814
return VM_FAULT_SIGBUS;
815815
}
816816

817+
void kvm_arch_free_memslot(struct kvm_memory_slot *free,
818+
struct kvm_memory_slot *dont)
819+
{
820+
}
821+
822+
int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
823+
{
824+
return 0;
825+
}
826+
817827
/* Section: memory related */
818828
int kvm_arch_prepare_memory_region(struct kvm *kvm,
819829
struct kvm_memory_slot *memslot,

arch/x86/include/asm/kvm_host.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,15 @@ struct kvm_vcpu_arch {
483483
} osvw;
484484
};
485485

486+
struct kvm_lpage_info {
487+
unsigned long rmap_pde;
488+
int write_count;
489+
};
490+
491+
struct kvm_arch_memory_slot {
492+
struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
493+
};
494+
486495
struct kvm_arch {
487496
unsigned int n_used_mmu_pages;
488497
unsigned int n_requested_mmu_pages;

arch/x86/kvm/mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
689689
unsigned long idx;
690690

691691
idx = gfn_to_index(gfn, slot->base_gfn, level);
692-
return &slot->lpage_info[level - 2][idx];
692+
return &slot->arch.lpage_info[level - 2][idx];
693693
}
694694

695695
static void account_shadowed(struct kvm *kvm, gfn_t gfn)

arch/x86/kvm/x86.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6239,6 +6239,65 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
62396239
put_page(kvm->arch.ept_identity_pagetable);
62406240
}
62416241

6242+
void kvm_arch_free_memslot(struct kvm_memory_slot *free,
6243+
struct kvm_memory_slot *dont)
6244+
{
6245+
int i;
6246+
6247+
for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
6248+
if (!dont || free->arch.lpage_info[i] != dont->arch.lpage_info[i]) {
6249+
vfree(free->arch.lpage_info[i]);
6250+
free->arch.lpage_info[i] = NULL;
6251+
}
6252+
}
6253+
}
6254+
6255+
int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
6256+
{
6257+
int i;
6258+
6259+
for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
6260+
unsigned long ugfn;
6261+
int lpages;
6262+
int level = i + 2;
6263+
6264+
lpages = gfn_to_index(slot->base_gfn + npages - 1,
6265+
slot->base_gfn, level) + 1;
6266+
6267+
slot->arch.lpage_info[i] =
6268+
vzalloc(lpages * sizeof(*slot->arch.lpage_info[i]));
6269+
if (!slot->arch.lpage_info[i])
6270+
goto out_free;
6271+
6272+
if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
6273+
slot->arch.lpage_info[i][0].write_count = 1;
6274+
if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
6275+
slot->arch.lpage_info[i][lpages - 1].write_count = 1;
6276+
ugfn = slot->userspace_addr >> PAGE_SHIFT;
6277+
/*
6278+
* If the gfn and userspace address are not aligned wrt each
6279+
* other, or if explicitly asked to, disable large page
6280+
* support for this slot
6281+
*/
6282+
if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
6283+
!kvm_largepages_enabled()) {
6284+
unsigned long j;
6285+
6286+
for (j = 0; j < lpages; ++j)
6287+
slot->arch.lpage_info[i][j].write_count = 1;
6288+
}
6289+
}
6290+
6291+
return 0;
6292+
6293+
out_free:
6294+
for (i = 0; i < KVM_NR_PAGE_SIZES - 1; ++i) {
6295+
vfree(slot->arch.lpage_info[i]);
6296+
slot->arch.lpage_info[i] = NULL;
6297+
}
6298+
return -ENOMEM;
6299+
}
6300+
62426301
int kvm_arch_prepare_memory_region(struct kvm *kvm,
62436302
struct kvm_memory_slot *memslot,
62446303
struct kvm_memory_slot old,

include/linux/kvm_host.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
171171
*/
172172
#define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
173173

174-
struct kvm_lpage_info {
175-
unsigned long rmap_pde;
176-
int write_count;
177-
};
178-
179174
struct kvm_memory_slot {
180175
gfn_t base_gfn;
181176
unsigned long npages;
@@ -184,7 +179,7 @@ struct kvm_memory_slot {
184179
unsigned long *dirty_bitmap;
185180
unsigned long *dirty_bitmap_head;
186181
unsigned long nr_dirty_pages;
187-
struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
182+
struct kvm_arch_memory_slot arch;
188183
unsigned long userspace_addr;
189184
int user_alloc;
190185
int id;
@@ -376,6 +371,9 @@ int kvm_set_memory_region(struct kvm *kvm,
376371
int __kvm_set_memory_region(struct kvm *kvm,
377372
struct kvm_userspace_memory_region *mem,
378373
int user_alloc);
374+
void kvm_arch_free_memslot(struct kvm_memory_slot *free,
375+
struct kvm_memory_slot *dont);
376+
int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages);
379377
int kvm_arch_prepare_memory_region(struct kvm *kvm,
380378
struct kvm_memory_slot *memslot,
381379
struct kvm_memory_slot old,
@@ -385,6 +383,7 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,
385383
struct kvm_userspace_memory_region *mem,
386384
struct kvm_memory_slot old,
387385
int user_alloc);
386+
bool kvm_largepages_enabled(void);
388387
void kvm_disable_largepages(void);
389388
void kvm_arch_flush_shadow(struct kvm *kvm);
390389

0 commit comments

Comments
 (0)