Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1935823ima; Thu, 25 Oct 2018 07:15:50 -0700 (PDT) X-Google-Smtp-Source: AJdET5epRwf/x5dHncZ2rklnIkZpzhFn9Jl9k5R8M8UWQhOyfxli+qO/TJu6K836MpOIk5LzXcBh X-Received: by 2002:a62:425b:: with SMTP id p88-v6mr1770115pfa.96.1540476950467; Thu, 25 Oct 2018 07:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540476950; cv=none; d=google.com; s=arc-20160816; b=Nsj3N71iBQY1wf2oAO5zVERger+9XV8f9HlLKocGraEcrOhoZOU7Z8b1V4d2fCdu0h SDAjuAmSY+Q0NFpCFV2tQkW7IU4qp58I5lV0yE808V2w7QlVOM62dESIO2g3cFj3LDnW XBKiBymb7pQa7O11ni0mDgE327r1QhfN1F4Su5K33RPrplYre8pOR+guiIF2b0kRL+ae gUPXg0wviuA9Z3nXKu3D9gsEfP7Uy9XOsaPLKg96WYzbDfRQI9DrmaBYRFhL5bIuRNic VH+nqz3+u164SBasswJFMoOsSJPL+0dUjDWveA6zps9xx4IYHgQnAxSgRVCgK8X1sAwi IhPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5AdTQbYtS5RHNuPqyowoBeLB1YDIlz/fJaz4UKYwogo=; b=WICj/gd12Jfx23vNMNPyr8uSpvOh2s2arw8PRhU/u2X9KvYJhzjoXMJhT0Rd/LmGAb VjO/TsZrQYVDsJYPlb1F2SnkkQHKkZe+WEa7Mhslnd6jEvb7EBbIDbM4nCj8Mpn3owk9 rl2qssdOWAP0g5Dy+ZVA9oGN/WgR02f4U3re2d+CyjRKzZWU2Q5AYezIQrfh6HtXtyST Nj2BEOL2la6nKTQYJzj0I9LnTNxOpjsvh1BokdTb94HR80JP2rLiDx3G1/uyVvbkSgKU u0ovtXQf0l2HjsurXBaEh1kaaNGcCMaTyss667ltNen3aV79FDxDZtODN8yiR3ca/q3P mCGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MdOR2Xjg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n73-v6si8572753pfh.96.2018.10.25.07.14.58; Thu, 25 Oct 2018 07:15:50 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=MdOR2Xjg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728162AbeJYWoZ (ORCPT + 99 others); Thu, 25 Oct 2018 18:44:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:53156 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728144AbeJYWoY (ORCPT ); Thu, 25 Oct 2018 18:44:24 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7993320856; Thu, 25 Oct 2018 14:11:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540476689; bh=ryFFmc8m3IhT39Uair1ODzkgWhAmmyNxWRd96wOD5dg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MdOR2Xjgd+rjzgj1mPDW1mSdAqkcSCfNemI80fOsLsXQDAID72OmGa9fmDz/5d/rX EKKauJWNqUBRpWhveZ78Qg6m9Y0GNby0DdsCCntoXx8HK1RK6eMGNPoZBgbiIhTtpA 7/VW+EjZ3WBcCNMMeSrRPezLgrg8aQJKFPhLPj5g= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: KarimAllah Ahmed , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH AUTOSEL 4.14 24/46] KVM: x86: Update the exit_qualification access bits while walking an address Date: Thu, 25 Oct 2018 10:10:31 -0400 Message-Id: <20181025141053.213330-24-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025141053.213330-1-sashal@kernel.org> References: <20181025141053.213330-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KarimAllah Ahmed [ Upstream commit ddd6f0e94d3153951580d5b88b9d97c7e26a0e00 ] ... to avoid having a stale value when handling an EPT misconfig for MMIO regions. MMIO regions that are not passed-through to the guest are handled through EPT misconfigs. The first time a certain MMIO page is touched it causes an EPT violation, then KVM marks the EPT entry to cause an EPT misconfig instead. Any subsequent accesses to the entry will generate an EPT misconfig. Things gets slightly complicated with nested guest handling for MMIO regions that are not passed through from L0 (i.e. emulated by L0 user-space). An EPT violation for one of these MMIO regions from L2, exits to L0 hypervisor. L0 would then look at the EPT12 mapping for L1 hypervisor and realize it is not present (or not sufficient to serve the request). Then L0 injects an EPT violation to L1. L1 would then update its EPT mappings. The EXIT_QUALIFICATION value for L1 would come from exit_qualification variable in "struct vcpu". The problem is that this variable is only updated on EPT violation and not on EPT misconfig. So if an EPT violation because of a read happened first, then an EPT misconfig because of a write happened afterwards. The L0 hypervisor will still contain exit_qualification value from the previous read instead of the write and end up injecting an EPT violation to the L1 hypervisor with an out of date EXIT_QUALIFICATION. The EPT violation that is injected from L0 to L1 needs to have the correct EXIT_QUALIFICATION specially for the access bits because the individual access bits for MMIO EPTs are updated only on actual access of this specific type. So for the example above, the L1 hypervisor will keep updating only the read bit in the EPT then resume the L2 guest. The L2 guest would end up causing another exit where the L0 *again* will inject another EPT violation to L1 hypervisor with *again* an out of date exit_qualification which indicates a read and not a write. Then this ping-pong just keeps happening without making any forward progress. The behavior of mapping MMIO regions changed in: commit a340b3e229b24 ("kvm: Map PFN-type memory regions as writable (if possible)") ... where an EPT violation for a read would also fixup the write bits to avoid another EPT violation which by acciddent would fix the bug mentioned above. This commit fixes this situation and ensures that the access bits for the exit_qualifcation is up to date. That ensures that even L1 hypervisor running with a KVM version before the commit mentioned above would still work. ( The description above assumes EPT to be available and used by L1 hypervisor + the L1 hypervisor is passing through the MMIO region to the L2 guest while this MMIO region is emulated by the L0 user-space ). Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Thomas Gleixner Cc: Ingo Molnar Cc: H. Peter Anvin Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: KarimAllah Ahmed Signed-off-by: Radim Krčmář Signed-off-by: Sasha Levin --- arch/x86/kvm/paging_tmpl.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/paging_tmpl.h b/arch/x86/kvm/paging_tmpl.h index 5abae72266b7..6288e9d7068e 100644 --- a/arch/x86/kvm/paging_tmpl.h +++ b/arch/x86/kvm/paging_tmpl.h @@ -452,14 +452,21 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, * done by is_rsvd_bits_set() above. * * We set up the value of exit_qualification to inject: - * [2:0] - Derive from [2:0] of real exit_qualification at EPT violation + * [2:0] - Derive from the access bits. The exit_qualification might be + * out of date if it is serving an EPT misconfiguration. * [5:3] - Calculated by the page walk of the guest EPT page tables * [7:8] - Derived from [7:8] of real exit_qualification * * The other bits are set to 0. */ if (!(errcode & PFERR_RSVD_MASK)) { - vcpu->arch.exit_qualification &= 0x187; + vcpu->arch.exit_qualification &= 0x180; + if (write_fault) + vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_WRITE; + if (user_fault) + vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_READ; + if (fetch_fault) + vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_INSTR; vcpu->arch.exit_qualification |= (pte_access & 0x7) << 3; } #endif -- 2.17.1