Received: by 10.223.185.116 with SMTP id b49csp2218397wrg; Thu, 15 Feb 2018 08:16:13 -0800 (PST) X-Google-Smtp-Source: AH8x226dGWe45hpvPFPJHUPtoS+Vo+1byrfhXDn1msbnrnpELtcvoN9Oox3pPHhy95tToCI01abq X-Received: by 10.99.142.76 with SMTP id k73mr2612053pge.278.1518711373513; Thu, 15 Feb 2018 08:16:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518711373; cv=none; d=google.com; s=arc-20160816; b=FJz1IPTCttKb19ufjyBn2UhIl3mtD1UgW7XL5PbwLs3MaHYB9DyQWcDkAeKWj2qfjG NpJiBd+l78KCn8WyWx7DWEWD/Vbyts24PefJgzBeO20YuiSdHhEJ91JVSoqFVcSyMyK1 KdCizszJ47jUIJL3GJmpnnYjyBI/dLA48VR80gitF6edUzLp63ruQV9ushJlemXg+Mbc eKwXrV1mEhd0qGvGOg9NaqQhcF9DUCBmRC2ITmw82D3KOFD5mOUF39cenVCq9MD8P90L Mx2CEJB6nHmJLf3xBM416w92jDpgVMUVj7fbvjpqTUEU8wa3b0OVH0TZNKswTCm2OwX8 5z2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=GtbiYsPAUwcL/0lRb2XDxhtbGGr+McaWjqTp3Tu/ICc=; b=vBJKe6VFOygcsZ89SjHtCT44ozWXOV5itH1N/HF/dwfygh7qx+f1o18utBkJREBe6E qBbezDrHuILwFUCsQo3l1bC1k4+GNdgUCHOuA1IHkuuuLtUQr9lHZ7vpjUjz0if6hPTp EcrSYnpvFS3+7pnzNZ2gZ35jmgh4/dVvRgmvVOOPujL30j7t19CO6M8TBfcUD1D8mL0d qRhGCe+N5KaHOMP4vOB94Ew4nPHBC0DxSZU5exDniQFN+gMpcpRz3givZPzCjp1SPvA0 LYtv2Vob1KYgXDnjWYAvP+ZhxYA2eyzlKjR9onr1ByHugzAHh913pbjyTfCLcfNuiS7Q QZSQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i75si6787116pfi.398.2018.02.15.08.15.59; Thu, 15 Feb 2018 08:16:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1424327AbeBOPop (ORCPT + 99 others); Thu, 15 Feb 2018 10:44:45 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:34434 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1423899AbeBOPom (ORCPT ); Thu, 15 Feb 2018 10:44:42 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id E7EFD11A6; Thu, 15 Feb 2018 15:44:41 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Mackerras Subject: [PATCH 4.15 134/202] KVM: PPC: Book3S HV: Drop locks before reading guest memory Date: Thu, 15 Feb 2018 16:17:14 +0100 Message-Id: <20180215151720.160232830@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151712.768794354@linuxfoundation.org> References: <20180215151712.768794354@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Paul Mackerras commit 36ee41d161c67a6fcf696d4817a0da31f778938c upstream. Running with CONFIG_DEBUG_ATOMIC_SLEEP reveals that HV KVM tries to read guest memory, in order to emulate guest instructions, while preempt is disabled and a vcore lock is held. This occurs in kvmppc_handle_exit_hv(), called from post_guest_process(), when emulating guest doorbell instructions on POWER9 systems, and also when checking whether we have hit a hypervisor breakpoint. Reading guest memory can cause a page fault and thus cause the task to sleep, so we need to avoid reading guest memory while holding a spinlock or when preempt is disabled. To fix this, we move the preempt_enable() in kvmppc_run_core() to before the loop that calls post_guest_process() for each vcore that has just run, and we drop and re-take the vcore lock around the calls to kvmppc_emulate_debug_inst() and kvmppc_emulate_doorbell_instr(). Dropping the lock is safe with respect to the iteration over the runnable vcpus in post_guest_process(); for_each_runnable_thread is actually safe to use locklessly. It is possible for a vcpu to become runnable and add itself to the runnable_threads array (code near the beginning of kvmppc_run_vcpu()) and then get included in the iteration in post_guest_process despite the fact that it has not just run. This is benign because vcpu->arch.trap and vcpu->arch.ceded will be zero. Fixes: 579006944e0d ("KVM: PPC: Book3S HV: Virtualize doorbell facility on POWER9") Signed-off-by: Paul Mackerras Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/kvm/book3s_hv.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -1005,8 +1005,6 @@ static int kvmppc_emulate_doorbell_instr struct kvm *kvm = vcpu->kvm; struct kvm_vcpu *tvcpu; - if (!cpu_has_feature(CPU_FTR_ARCH_300)) - return EMULATE_FAIL; if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE) return RESUME_GUEST; if (get_op(inst) != 31) @@ -1056,6 +1054,7 @@ static int kvmppc_emulate_doorbell_instr return RESUME_GUEST; } +/* Called with vcpu->arch.vcore->lock held */ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, struct task_struct *tsk) { @@ -1176,7 +1175,10 @@ static int kvmppc_handle_exit_hv(struct swab32(vcpu->arch.emul_inst) : vcpu->arch.emul_inst; if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { + /* Need vcore unlocked to call kvmppc_get_last_inst */ + spin_unlock(&vcpu->arch.vcore->lock); r = kvmppc_emulate_debug_inst(run, vcpu); + spin_lock(&vcpu->arch.vcore->lock); } else { kvmppc_core_queue_program(vcpu, SRR1_PROGILL); r = RESUME_GUEST; @@ -1191,8 +1193,13 @@ static int kvmppc_handle_exit_hv(struct */ case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: r = EMULATE_FAIL; - if ((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) + if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) && + cpu_has_feature(CPU_FTR_ARCH_300)) { + /* Need vcore unlocked to call kvmppc_get_last_inst */ + spin_unlock(&vcpu->arch.vcore->lock); r = kvmppc_emulate_doorbell_instr(vcpu); + spin_lock(&vcpu->arch.vcore->lock); + } if (r == EMULATE_FAIL) { kvmppc_core_queue_program(vcpu, SRR1_PROGILL); r = RESUME_GUEST; @@ -2934,13 +2941,14 @@ static noinline void kvmppc_run_core(str /* make sure updates to secondary vcpu structs are visible now */ smp_mb(); + preempt_enable(); + for (sub = 0; sub < core_info.n_subcores; ++sub) { pvc = core_info.vc[sub]; post_guest_process(pvc, pvc == vc); } spin_lock(&vc->lock); - preempt_enable(); out: vc->vcore_state = VCORE_INACTIVE;