Received: by 10.223.185.116 with SMTP id b49csp23021wrg; Thu, 15 Feb 2018 15:46:16 -0800 (PST) X-Google-Smtp-Source: AH8x2259ovWxigyAT4RH5a0LsADQoc0sqw2kxzeFY7sbGje/sj7jOUnqO4EKDiZKfYsc84BgyWLp X-Received: by 10.101.73.12 with SMTP id p12mr3640483pgs.442.1518738376604; Thu, 15 Feb 2018 15:46:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518738376; cv=none; d=google.com; s=arc-20160816; b=mJ/kEuKo3iGWGVU0MEb7CxabUzwhGAIXi8wRnLFRe3sVzYOgW+zS+PX4WZgph1RFCY Hm5bMi4lMvhaZRiTJEM74+zy6+w0XVCKEJoQ+Tfrh29xDjdJvurXssVlmvUVhsOhB5aO 1ZN/x8LmC3Ct5/iTPiO3OOFgTx3q0UqFfhedPfj7Ka4/cJTdIDbOsKtoJhrNRSctyZum PYnd4FPKs/xC6MkQJV2c04zPKwQP+omhirDlrz18u49SHymf2WfadStZcJQVWYMsIb43 OddOU4pwr9vYlRCxMdVIxsqqAogu3GGR97GHEg1pBm6xSer0pOj0SJ98jjpvb0+bJtav q0YQ== 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=7BK7f1ul03aRgzqPimsH+ezci+QdHy+6jBEkE/OxoEI=; b=PAbr62WHPcnrTZKP5+XLMsL6GuRuO8VNqZyzQrtHnR1oTeApY2RpSq1l6pT4aURWKp PuzURWpfe4MNzDdOZCHFaXNAeiTKTQg1aHvgB72MezhX69TZ+/n8LeRFKdtHV2wJB5g7 tRPAq9YIXhPvQv8D3DbBRtnPC+4Tpf9Y7+Mubo3yITurq1o4aRWNcAzk7qwY3P5s6orJ mIh0b0E4HtJPazWNeTul+odRXkhm1OWp/+nAldL7lTVy6l/r+NLpgFiaKS4zlY6a3Xz3 T0jgytUOjp42s0LJomi7tsBc0o4plvHOCKlWyzK6FRqleuGNLlvNrxBVlfaW9JxvwIlj Bzew== 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 x27si226093pgc.25.2018.02.15.15.46.02; Thu, 15 Feb 2018 15:46:16 -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 S1422786AbeBOPfz (ORCPT + 99 others); Thu, 15 Feb 2018 10:35:55 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:57594 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1422724AbeBOPfw (ORCPT ); Thu, 15 Feb 2018 10:35:52 -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 237671155; Thu, 15 Feb 2018 15:35:51 +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.14 140/195] KVM: PPC: Book3S HV: Drop locks before reading guest memory Date: Thu, 15 Feb 2018 16:17:11 +0100 Message-Id: <20180215151712.792026657@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151705.738773577@linuxfoundation.org> References: <20180215151705.738773577@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.14-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 @@ -999,8 +999,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) @@ -1050,6 +1048,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) { @@ -1169,7 +1168,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; @@ -1184,8 +1186,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; @@ -2889,13 +2896,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;