From patchwork Tue Aug 2 14:34:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9257791 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B12C660754 for ; Tue, 2 Aug 2016 14:42:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A10C02860E for ; Tue, 2 Aug 2016 14:42:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95BE428611; Tue, 2 Aug 2016 14:42:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE98528615 for ; Tue, 2 Aug 2016 14:42:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755248AbcHBOmO (ORCPT ); Tue, 2 Aug 2016 10:42:14 -0400 Received: from foss.arm.com ([217.140.101.70]:39854 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756775AbcHBOdv (ORCPT ); Tue, 2 Aug 2016 10:33:51 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8AB4A28; Tue, 2 Aug 2016 07:35:11 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E70523F387; Tue, 2 Aug 2016 07:33:48 -0700 (PDT) From: Andre Przywara To: Marc Zyngier , Christoffer Dall Cc: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Stefan Agner , Andrew Jones Subject: [PATCH] KVM: arm/arm64: initialize new VGIC even without host GIC Date: Tue, 2 Aug 2016 15:34:45 +0100 Message-Id: <20160802143445.14005-1-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The new VGIC code is always in the VCPU entry/exit path, even when the actual GIC hardware initialization found the VGIC unusable (due to non-aligned base addresses or a missing maintenance interrupt, for instance). Since in this case the VGIC structures aren't initialized properly, the host kernel crashes on a NULL pointer dereference. Initialize each VCPU's ap_list even with the VGIC unavailable, so the VGIC code now just iterates an empty list in that case and no longer crashes the kernel. Also take care of the arch timer (which becomes unusable as well without a VGIC) by using a static key to prevent arch timer code to run in the hot path. Much of the code was inspired by Marc Zyngier. Signed-off-by: Andre Przywara Reported-by: Stefan Agner Tested-by: Stefan Agner --- Hi Stefan, Drew, can you test this on your systems which showed the issue? Cheers, Andre. arch/arm/kvm/arm.c | 14 ++++++++++---- include/kvm/arm_arch_timer.h | 2 ++ virt/kvm/arm/arch_timer.c | 11 +++++++++++ virt/kvm/arm/vgic/vgic-init.c | 9 ++++----- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index f1bde7c..cef35ce 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -294,7 +294,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); /* Set up the timer */ - kvm_timer_vcpu_init(vcpu); + if (vgic_present) + kvm_timer_vcpu_init(vcpu); kvm_arm_reset_debug_ptr(vcpu); @@ -1208,6 +1209,7 @@ static int init_subsystems(void) break; case -ENODEV: case -ENXIO: + kvm_err("No useable vgic detected\n"); vgic_present = false; err = 0; break; @@ -1218,9 +1220,13 @@ static int init_subsystems(void) /* * Init HYP architected timer support */ - err = kvm_timer_hyp_init(); - if (err) - goto out; + if (vgic_present) { + err = kvm_timer_hyp_init(); + if (err) + goto out; + } else { + static_branch_enable(&kvm_arch_timer_disabled); + } kvm_perf_init(); kvm_coproc_table_init(); diff --git a/include/kvm/arm_arch_timer.h b/include/kvm/arm_arch_timer.h index dda39d8..64f361f 100644 --- a/include/kvm/arm_arch_timer.h +++ b/include/kvm/arm_arch_timer.h @@ -23,6 +23,8 @@ #include #include +extern struct static_key_false kvm_arch_timer_disabled; + struct arch_timer_kvm { /* Virtual offset */ cycle_t cntvoff; diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c index 4fde8c7..71fb04c 100644 --- a/virt/kvm/arm/arch_timer.c +++ b/virt/kvm/arm/arch_timer.c @@ -34,6 +34,8 @@ static struct timecounter *timecounter; static struct workqueue_struct *wqueue; static unsigned int host_vtimer_irq; +DEFINE_STATIC_KEY_FALSE(kvm_arch_timer_disabled); + void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) { vcpu->arch.timer_cpu.active_cleared_last = false; @@ -41,6 +43,9 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) static cycle_t kvm_phys_timer_read(void) { + if (static_branch_unlikely(&kvm_arch_timer_disabled)) + return 0; + return timecounter->cc->read(timecounter->cc); } @@ -104,6 +109,9 @@ static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu) { cycle_t cval, now; + if (static_branch_unlikely(&kvm_arch_timer_disabled)) + return 0; + cval = vcpu->arch.timer_cpu.cntv_cval; now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff; @@ -148,6 +156,9 @@ static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; + if (static_branch_unlikely(&kvm_arch_timer_disabled)) + return false; + return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) && (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE); } diff --git a/virt/kvm/arm/vgic/vgic-init.c b/virt/kvm/arm/vgic/vgic-init.c index 2c7f0d5..b92133c 100644 --- a/virt/kvm/arm/vgic/vgic-init.c +++ b/virt/kvm/arm/vgic/vgic-init.c @@ -56,6 +56,10 @@ void kvm_vgic_early_init(struct kvm *kvm) void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu) { + struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + + INIT_LIST_HEAD(&vgic_cpu->ap_list_head); + spin_lock_init(&vgic_cpu->ap_list_lock); } /* CREATION */ @@ -392,11 +396,6 @@ int kvm_vgic_hyp_init(void) if (!gic_kvm_info) return -ENODEV; - if (!gic_kvm_info->maint_irq) { - kvm_err("No vgic maintenance irq\n"); - return -ENXIO; - } - switch (gic_kvm_info->type) { case GIC_V2: ret = vgic_v2_probe(gic_kvm_info);