Skip to content

Commit e329fb7

Browse files
christofferdall-armMarc Zyngier
authored andcommitted
KVM: arm/arm64: Factor out VMID into struct kvm_vmid
In preparation for nested virtualization where we are going to have more than a single VMID per VM, let's factor out the VMID data into a separate VMID data structure and change the VMID allocator to operate on this new structure instead of using a struct kvm. This also means that udate_vttbr now becomes update_vmid, and that the vttbr itself is generated on the fly based on the stage 2 page table base address and the vmid. We cache the physical address of the pgd when allocating the pgd to avoid doing the calculation on every entry to the guest and to avoid calling into potentially non-hyp-mapped code from hyp/EL2. If we wanted to merge the VMID allocator with the arm64 ASID allocator at some point in the future, it should actually become easier to do that after this patch. Note that to avoid mapping the kvm_vmid_bits variable into hyp, we simply forego the masking of the vmid value in kvm_get_vttbr and rely on update_vmid to always assign a valid vmid value (within the supported range). Reviewed-by: Marc Zyngier <marc.zyngier@arm.com> [maz: minor cleanups] Reviewed-by: Julien Thierry <julien.thierry@arm.com> Signed-off-by: Christoffer Dall <christoffer.dall@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 32f1395 commit e329fb7

9 files changed

Lines changed: 61 additions & 53 deletions

File tree

‎arch/arm/include/asm/kvm_host.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ int __attribute_const__ kvm_target_cpu(void);
5757
int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
5858
void kvm_reset_coprocs(struct kvm_vcpu *vcpu);
5959

60-
struct kvm_arch {
61-
/* VTTBR value associated with below pgd and vmid */
62-
u64 vttbr;
60+
struct kvm_vmid {
61+
/* The VMID generation used for the virt. memory system */
62+
u64 vmid_gen;
63+
u32 vmid;
64+
};
6365

66+
struct kvm_arch {
6467
/* The last vcpu id that ran on each physical CPU */
6568
int __percpu *last_vcpu_ran;
6669

@@ -70,11 +73,11 @@ struct kvm_arch {
7073
*/
7174

7275
/* The VMID generation used for the virt. memory system */
73-
u64 vmid_gen;
74-
u32 vmid;
76+
struct kvm_vmid vmid;
7577

7678
/* Stage-2 page table */
7779
pgd_t *pgd;
80+
phys_addr_t pgd_phys;
7881

7982
/* Interrupt controller */
8083
struct vgic_dist vgic;

‎arch/arm/include/asm/kvm_mmu.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,14 @@ static inline int hyp_map_aux_data(void)
421421

422422
static inline void kvm_set_ipa_limit(void) {}
423423

424-
static inline bool kvm_cpu_has_cnp(void)
424+
static __always_inline u64 kvm_get_vttbr(struct kvm *kvm)
425425
{
426-
return false;
426+
struct kvm_vmid *vmid = &kvm->arch.vmid;
427+
u64 vmid_field, baddr;
428+
429+
baddr = kvm->arch.pgd_phys;
430+
vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT;
431+
return kvm_phys_to_vttbr(baddr) | vmid_field;
427432
}
428433

429434
#endif /* !__ASSEMBLY__ */

‎arch/arm/kvm/hyp/switch.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
7777
static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
7878
{
7979
struct kvm *kvm = kern_hyp_va(vcpu->kvm);
80-
write_sysreg(kvm->arch.vttbr, VTTBR);
80+
write_sysreg(kvm_get_vttbr(kvm), VTTBR);
8181
write_sysreg(vcpu->arch.midr, VPIDR);
8282
}
8383

‎arch/arm/kvm/hyp/tlb.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
4141

4242
/* Switch to requested VMID */
4343
kvm = kern_hyp_va(kvm);
44-
write_sysreg(kvm->arch.vttbr, VTTBR);
44+
write_sysreg(kvm_get_vttbr(kvm), VTTBR);
4545
isb();
4646

4747
write_sysreg(0, TLBIALLIS);
@@ -61,7 +61,7 @@ void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
6161
struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
6262

6363
/* Switch to requested VMID */
64-
write_sysreg(kvm->arch.vttbr, VTTBR);
64+
write_sysreg(kvm_get_vttbr(kvm), VTTBR);
6565
isb();
6666

6767
write_sysreg(0, TLBIALL);

‎arch/arm64/include/asm/kvm_host.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
5757
int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext);
5858
void __extended_idmap_trampoline(phys_addr_t boot_pgd, phys_addr_t idmap_start);
5959

60-
struct kvm_arch {
60+
struct kvm_vmid {
6161
/* The VMID generation used for the virt. memory system */
6262
u64 vmid_gen;
6363
u32 vmid;
64+
};
65+
66+
struct kvm_arch {
67+
struct kvm_vmid vmid;
6468

6569
/* stage2 entry level table */
6670
pgd_t *pgd;
71+
phys_addr_t pgd_phys;
6772

68-
/* VTTBR value associated with above pgd and vmid */
69-
u64 vttbr;
7073
/* VTCR_EL2 value for this VM */
7174
u64 vtcr;
7275

‎arch/arm64/include/asm/kvm_hyp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/compiler.h>
2222
#include <linux/kvm_host.h>
2323
#include <asm/alternative.h>
24+
#include <asm/kvm_mmu.h>
2425
#include <asm/sysreg.h>
2526

2627
#define __hyp_text __section(.hyp.text) notrace
@@ -163,7 +164,7 @@ void __noreturn __hyp_do_panic(unsigned long, ...);
163164
static __always_inline void __hyp_text __load_guest_stage2(struct kvm *kvm)
164165
{
165166
write_sysreg(kvm->arch.vtcr, vtcr_el2);
166-
write_sysreg(kvm->arch.vttbr, vttbr_el2);
167+
write_sysreg(kvm_get_vttbr(kvm), vttbr_el2);
167168

168169
/*
169170
* ARM erratum 1165522 requires the actual execution of the above

‎arch/arm64/include/asm/kvm_mmu.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,15 @@ static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm)
591591
return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm));
592592
}
593593

594-
static inline bool kvm_cpu_has_cnp(void)
594+
static __always_inline u64 kvm_get_vttbr(struct kvm *kvm)
595595
{
596-
return system_supports_cnp();
596+
struct kvm_vmid *vmid = &kvm->arch.vmid;
597+
u64 vmid_field, baddr;
598+
u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0;
599+
600+
baddr = kvm->arch.pgd_phys;
601+
vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT;
602+
return kvm_phys_to_vttbr(baddr) | vmid_field | cnp;
597603
}
598604

599605
#endif /* __ASSEMBLY__ */

‎virt/kvm/arm/arm.c‎

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu);
6565
/* The VMID used in the VTTBR */
6666
static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
6767
static u32 kvm_next_vmid;
68-
static unsigned int kvm_vmid_bits __read_mostly;
6968
static DEFINE_SPINLOCK(kvm_vmid_lock);
7069

7170
static bool vgic_present;
@@ -142,7 +141,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
142141
kvm_vgic_early_init(kvm);
143142

144143
/* Mark the initial VMID generation invalid */
145-
kvm->arch.vmid_gen = 0;
144+
kvm->arch.vmid.vmid_gen = 0;
146145

147146
/* The maximum number of VCPUs is limited by the host's GIC model */
148147
kvm->arch.max_vcpus = vgic_present ?
@@ -472,37 +471,31 @@ void force_vm_exit(const cpumask_t *mask)
472471

473472
/**
474473
* need_new_vmid_gen - check that the VMID is still valid
475-
* @kvm: The VM's VMID to check
474+
* @vmid: The VMID to check
476475
*
477476
* return true if there is a new generation of VMIDs being used
478477
*
479-
* The hardware supports only 256 values with the value zero reserved for the
480-
* host, so we check if an assigned value belongs to a previous generation,
481-
* which which requires us to assign a new value. If we're the first to use a
482-
* VMID for the new generation, we must flush necessary caches and TLBs on all
483-
* CPUs.
478+
* The hardware supports a limited set of values with the value zero reserved
479+
* for the host, so we check if an assigned value belongs to a previous
480+
* generation, which which requires us to assign a new value. If we're the
481+
* first to use a VMID for the new generation, we must flush necessary caches
482+
* and TLBs on all CPUs.
484483
*/
485-
static bool need_new_vmid_gen(struct kvm *kvm)
484+
static bool need_new_vmid_gen(struct kvm_vmid *vmid)
486485
{
487486
u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
488487
smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
489-
return unlikely(READ_ONCE(kvm->arch.vmid_gen) != current_vmid_gen);
488+
return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
490489
}
491490

492491
/**
493-
* update_vttbr - Update the VTTBR with a valid VMID before the guest runs
494-
* @kvm The guest that we are about to run
495-
*
496-
* Called from kvm_arch_vcpu_ioctl_run before entering the guest to ensure the
497-
* VM has a valid VMID, otherwise assigns a new one and flushes corresponding
498-
* caches and TLBs.
492+
* update_vmid - Update the vmid with a valid VMID for the current generation
493+
* @kvm: The guest that struct vmid belongs to
494+
* @vmid: The stage-2 VMID information struct
499495
*/
500-
static void update_vttbr(struct kvm *kvm)
496+
static void update_vmid(struct kvm_vmid *vmid)
501497
{
502-
phys_addr_t pgd_phys;
503-
u64 vmid, cnp = kvm_cpu_has_cnp() ? VTTBR_CNP_BIT : 0;
504-
505-
if (!need_new_vmid_gen(kvm))
498+
if (!need_new_vmid_gen(vmid))
506499
return;
507500

508501
spin_lock(&kvm_vmid_lock);
@@ -512,7 +505,7 @@ static void update_vttbr(struct kvm *kvm)
512505
* already allocated a valid vmid for this vm, then this vcpu should
513506
* use the same vmid.
514507
*/
515-
if (!need_new_vmid_gen(kvm)) {
508+
if (!need_new_vmid_gen(vmid)) {
516509
spin_unlock(&kvm_vmid_lock);
517510
return;
518511
}
@@ -536,18 +529,12 @@ static void update_vttbr(struct kvm *kvm)
536529
kvm_call_hyp(__kvm_flush_vm_context);
537530
}
538531

539-
kvm->arch.vmid = kvm_next_vmid;
532+
vmid->vmid = kvm_next_vmid;
540533
kvm_next_vmid++;
541-
kvm_next_vmid &= (1 << kvm_vmid_bits) - 1;
542-
543-
/* update vttbr to be used with the new vmid */
544-
pgd_phys = virt_to_phys(kvm->arch.pgd);
545-
BUG_ON(pgd_phys & ~kvm_vttbr_baddr_mask(kvm));
546-
vmid = ((u64)(kvm->arch.vmid) << VTTBR_VMID_SHIFT) & VTTBR_VMID_MASK(kvm_vmid_bits);
547-
kvm->arch.vttbr = kvm_phys_to_vttbr(pgd_phys) | vmid | cnp;
534+
kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
548535

549536
smp_wmb();
550-
WRITE_ONCE(kvm->arch.vmid_gen, atomic64_read(&kvm_vmid_gen));
537+
WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
551538

552539
spin_unlock(&kvm_vmid_lock);
553540
}
@@ -690,7 +677,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
690677
*/
691678
cond_resched();
692679

693-
update_vttbr(vcpu->kvm);
680+
update_vmid(&vcpu->kvm->arch.vmid);
694681

695682
check_vcpu_requests(vcpu);
696683

@@ -739,7 +726,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
739726
*/
740727
smp_store_mb(vcpu->mode, IN_GUEST_MODE);
741728

742-
if (ret <= 0 || need_new_vmid_gen(vcpu->kvm) ||
729+
if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) ||
743730
kvm_request_pending(vcpu)) {
744731
vcpu->mode = OUTSIDE_GUEST_MODE;
745732
isb(); /* Ensure work in x_flush_hwstate is committed */
@@ -1417,10 +1404,6 @@ static inline void hyp_cpu_pm_exit(void)
14171404

14181405
static int init_common_resources(void)
14191406
{
1420-
/* set size of VMID supported by CPU */
1421-
kvm_vmid_bits = kvm_get_vmid_bits();
1422-
kvm_info("%d-bit VMID\n", kvm_vmid_bits);
1423-
14241407
kvm_set_ipa_limit();
14251408

14261409
return 0;

‎virt/kvm/arm/mmu.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
908908
*/
909909
int kvm_alloc_stage2_pgd(struct kvm *kvm)
910910
{
911+
phys_addr_t pgd_phys;
911912
pgd_t *pgd;
912913

913914
if (kvm->arch.pgd != NULL) {
@@ -920,7 +921,12 @@ int kvm_alloc_stage2_pgd(struct kvm *kvm)
920921
if (!pgd)
921922
return -ENOMEM;
922923

924+
pgd_phys = virt_to_phys(pgd);
925+
if (WARN_ON(pgd_phys & ~kvm_vttbr_baddr_mask(kvm)))
926+
return -EINVAL;
927+
923928
kvm->arch.pgd = pgd;
929+
kvm->arch.pgd_phys = pgd_phys;
924930
return 0;
925931
}
926932

@@ -1008,6 +1014,7 @@ void kvm_free_stage2_pgd(struct kvm *kvm)
10081014
unmap_stage2_range(kvm, 0, kvm_phys_size(kvm));
10091015
pgd = READ_ONCE(kvm->arch.pgd);
10101016
kvm->arch.pgd = NULL;
1017+
kvm->arch.pgd_phys = 0;
10111018
}
10121019
spin_unlock(&kvm->mmu_lock);
10131020

0 commit comments

Comments
 (0)