Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3698572pxv; Mon, 26 Jul 2021 09:39:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKB0FXaliye8KwCzw8YAlOtwCuKXsvGtULCJ6OWMMaDc8O1HrOOIJ/tKRaelPVh2/c7cEl X-Received: by 2002:a17:906:8890:: with SMTP id ak16mr1361533ejc.339.1627317508087; Mon, 26 Jul 2021 09:38:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627317508; cv=none; d=google.com; s=arc-20160816; b=rOSVD0vJq9JWyis7I62nmnjOveCsKeLCSF2rJT2krTuh0LAJo/E0r9jQghNM53Yx36 nuygOjHHvaVJAW6t4rfu9MRiHp9TeuYml6ZSH/vStes+XQek/9eEowJaXWAT4FnfX4M/ QcyUvLyCFpa1aiFBWFk97mM9DsmtHvoqjhkmAMNA1/69U7PRCt/95zpVpqSbawmyn159 bqyXrmF8+Xem0ExpeWRv07BOPWsnLzZEEbLCYjtKE2dprls809iVZhXK+7Vyv5qZWTfL hAtrjcFeVr0c773/63Rn5zV2Ngf3eQdT7QSaCEyhdVULJtLnQrAbsby9e8iPo50Ir/DT Mrng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/aeaa3I7KGswbdVBDhafvn+bz71hFC4cpcvl6jSHYT0=; b=JOuI0UBRYBEfj4gpmDRAfUFp5Cmon7HxAT43HWoaAg4Hyjd+roDNipi4hY5R9Ci/st hxxRZ78ofQjUllTBQIlSI1XAgBy6XPMclVcy08mIz7AtRksO6JggOqHbVNMhnBXNN46l vlpDPkwi2Aq/1VaXWOPw55th/5BWXvjQm42d5RxnseiscK995OGgtegPJh9ZHA7LiJEr yz6dHOwqEFBd3ZANwpJO69VHa7UTcLoEqSIh4dVOo/pQXILBopp/66q5bi9FuKiPzgEk GMOQH5nHC+UymSRG3gQI0GoT3BWPnVS8fU2iXZv+sXd6ucCzu/ctLrV57wdAuAdWc9DT qmsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=coA6KfHM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si348537ejo.665.2021.07.26.09.38.05; Mon, 26 Jul 2021 09:38:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=coA6KfHM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239853AbhGZPy3 (ORCPT + 99 others); Mon, 26 Jul 2021 11:54:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:50194 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234214AbhGZPdS (ORCPT ); Mon, 26 Jul 2021 11:33:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 614AF60F5D; Mon, 26 Jul 2021 16:13:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627316026; bh=LiSKxSE0Gq7jxtEWdMxJjtG11NBp++6rSqL7yERFtdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=coA6KfHMiH7wqAB8f8vHPuU0uhu45/WZs6ZZ9bJDn9B/428aAClyL4h7IwTodVoAi JGP7VnAByxF4F7wn8pbKHa5KytPX6djPRx1CPGpxOy7uCKlyM6fa8fgpi4tBKXHTsZ Q1eUKaGhdBE87WUvbINrgx/xb3AjfCw2kg83j7qg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Kardashevskiy , Michael Neuling , Nicholas Piggin , Michael Ellerman Subject: [PATCH 5.13 160/223] KVM: PPC: Book3S HV Nested: Sanitise H_ENTER_NESTED TM state Date: Mon, 26 Jul 2021 17:39:12 +0200 Message-Id: <20210726153851.445647683@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210726153846.245305071@linuxfoundation.org> References: <20210726153846.245305071@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicholas Piggin commit d9c57d3ed52a92536f5fa59dc5ccdd58b4875076 upstream. The H_ENTER_NESTED hypercall is handled by the L0, and it is a request by the L1 to switch the context of the vCPU over to that of its L2 guest, and return with an interrupt indication. The L1 is responsible for switching some registers to guest context, and the L0 switches others (including all the hypervisor privileged state). If the L2 MSR has TM active, then the L1 is responsible for recheckpointing the L2 TM state. Then the L1 exits to L0 via the H_ENTER_NESTED hcall, and the L0 saves the TM state as part of the exit, and then it recheckpoints the TM state as part of the nested entry and finally HRFIDs into the L2 with TM active MSR. Not efficient, but about the simplest approach for something that's horrendously complicated. Problems arise if the L1 exits to the L0 with a TM state which does not match the L2 TM state being requested. For example if the L1 is transactional but the L2 MSR is non-transactional, or vice versa. The L0's HRFID can take a TM Bad Thing interrupt and crash. Fix this by disallowing H_ENTER_NESTED in TM[T] state entirely, and then ensuring that if the L1 is suspended then the L2 must have TM active, and if the L1 is not suspended then the L2 must not have TM active. Fixes: 360cae313702 ("KVM: PPC: Book3S HV: Nested guest entry via hypercall") Cc: stable@vger.kernel.org # v4.20+ Reported-by: Alexey Kardashevskiy Acked-by: Michael Neuling Signed-off-by: Nicholas Piggin Signed-off-by: Michael Ellerman Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/kvm/book3s_hv_nested.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) --- a/arch/powerpc/kvm/book3s_hv_nested.c +++ b/arch/powerpc/kvm/book3s_hv_nested.c @@ -301,6 +301,9 @@ long kvmhv_enter_nested_guest(struct kvm if (vcpu->kvm->arch.l1_ptcr == 0) return H_NOT_AVAILABLE; + if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr)) + return H_BAD_MODE; + /* copy parameters in */ hv_ptr = kvmppc_get_gpr(vcpu, 4); regs_ptr = kvmppc_get_gpr(vcpu, 5); @@ -321,6 +324,23 @@ long kvmhv_enter_nested_guest(struct kvm if (l2_hv.vcpu_token >= NR_CPUS) return H_PARAMETER; + /* + * L1 must have set up a suspended state to enter the L2 in a + * transactional state, and only in that case. These have to be + * filtered out here to prevent causing a TM Bad Thing in the + * host HRFID. We could synthesize a TM Bad Thing back to the L1 + * here but there doesn't seem like much point. + */ + if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr)) { + if (!MSR_TM_ACTIVE(l2_regs.msr)) + return H_BAD_MODE; + } else { + if (l2_regs.msr & MSR_TS_MASK) + return H_BAD_MODE; + if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_TS_MASK)) + return H_BAD_MODE; + } + /* translate lpid */ l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true); if (!l2)