From: wanghaibin <wanghaibin.wang@huawei.com> Abstract kvm_vgic_vcpu_init() by moving it to the irqchip_flow structure. This results in a minor change of the way we initialize vcpus: VCPUs created prior to the creation of the vgic device don't have their local view of the vgic initialized. This means that on vgic instantiation, we must "catch up" and initialise the CPU interfaces for these vcpus. VCPUs created after the vgic device will follow the unusual flow. Special care must be taken to accomodate the different locking contexts though. The function can then be made static and the irqchip_in_kernel() test dropped, as we only get here if a vgic has been created. Signed-off-by: Marc Zyngier <maz@kernel.org> Signed-off-by: wanghaibin <wanghaibin.wang@huawei.com> --- arch/arm64/include/asm/kvm_irq.h | 4 ++++ arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 39 ++++++++++++++++++++++++++------ include/kvm/arm_vgic.h | 1 - 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/kvm_irq.h b/arch/arm64/include/asm/kvm_irq.h index 42d110155627..8fa29ede1835 100644 --- a/arch/arm64/include/asm/kvm_irq.h +++ b/arch/arm64/include/asm/kvm_irq.h @@ -20,6 +20,7 @@ enum kvm_irqchip_type { struct kvm_irqchip_flow { void (*irqchip_destroy)(struct kvm *); void (*irqchip_vcpu_destroy)(struct kvm_vcpu *vcpu); + int (*irqchip_vcpu_init)(struct kvm_vcpu *); }; /* @@ -55,4 +56,7 @@ struct kvm_irqchip_flow { #define kvm_irqchip_vcpu_destroy(v) \ __vcpu_irqchip_action((v), vcpu_destroy, (v)) +#define kvm_irqchip_vcpu_init(v) \ + __vcpu_irqchip_action_ret((v), vcpu_init, (v)) + #endif diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 9a21a064afb3..3fc15c48ec0c 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -636,7 +636,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; - err = kvm_vgic_vcpu_init(vcpu); + err = kvm_irqchip_vcpu_init(vcpu); if (err) return err; diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c index 0c8972404b79..45a5dc95a910 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -18,12 +18,14 @@ #include "hisilicon/hisi_virt.h" #endif +static int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu); static void kvm_vgic_destroy(struct kvm *kvm); static void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu); static struct kvm_irqchip_flow vgic_irqchip_flow = { .irqchip_destroy = kvm_vgic_destroy, .irqchip_vcpu_destroy = kvm_vgic_vcpu_destroy, + .irqchip_vcpu_init = kvm_vgic_vcpu_init, }; /* @@ -90,6 +92,8 @@ static void kvm_vgic_early_init(struct kvm *kvm) #endif } +static int __kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu); + /* CREATION */ /** @@ -158,8 +162,22 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) kvm_vgic_early_init(kvm); + out_unlock: mutex_unlock(&kvm->arch.config_lock); + + /* + * vcpus may have been created before the GIC. Initialize + * them. Careful that kvm->lock is held already on the + * KVM_CREATE_DEVICE path, so use the non-locking version. + * fix: need slot lock, use the lock vcpu init + */ + kvm_for_each_vcpu(i, vcpu, kvm) { + ret = kvm_vgic_vcpu_init(vcpu); + if (ret) + break; + } + unlock_all_vcpus(kvm); return ret; } @@ -225,7 +243,7 @@ static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis) * Only do initialization, but do not actually enable the * VGIC CPU interface */ -int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) +static int __kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) { struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; int ret = 0; @@ -265,18 +283,25 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) #endif } - if (!irqchip_in_kernel(vcpu->kvm)) - return 0; /* * If we are creating a VCPU with a GICv3 we must also register the * KVM io device for the redistributor that belongs to this VCPU. */ - if (irqchip_is_gic_v3(vcpu->kvm)) { - mutex_lock(&vcpu->kvm->slots_lock); + if (irqchip_is_gic_v3(vcpu->kvm)) ret = vgic_register_redist_iodev(vcpu); - mutex_unlock(&vcpu->kvm->slots_lock); - } + + return ret; +} + +static int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) +{ + int ret; + + mutex_lock(&vcpu->kvm->slots_lock); + ret = __kvm_vgic_vcpu_init(vcpu); + mutex_unlock(&vcpu->kvm->slots_lock); + return ret; } diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 6b650434e499..b6132c062725 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -442,7 +442,6 @@ extern struct static_key_false vgic_v2_cpuif_trap; extern struct static_key_false vgic_v3_cpuif_trap; int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev_addr); -int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu); int kvm_vgic_create(struct kvm *kvm, u32 type); int kvm_vgic_map_resources(struct kvm *kvm); int kvm_vgic_hyp_init(void); -- 2.33.0