Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp317921pxb; Thu, 5 Nov 2020 00:19:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbxVvhWoHNz+dUU6BvSiHLkyMUhT5UX1ijP6kbfLI9Y3kpEgbl+MU3HNj+woO5x1HPwks8 X-Received: by 2002:a17:906:7481:: with SMTP id e1mr1144730ejl.13.1604564385049; Thu, 05 Nov 2020 00:19:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604564385; cv=none; d=google.com; s=arc-20160816; b=Xs/rMODqdPXOkdHtLzWz8WgPIdUaGOj9pD+LXUMM8wFwUmFxyYSkJhnKzvRHUCOX8f qsqvjoFX6ldT5u5fGzTc0d1I4C/vuqikpf7FrXLrgQZiCNzH8ohCFwjjyEvBe48lja1d Awu2PUglbvtPJfn9VJw0XCup2wsXrlHKdnZ6fAAOHHcshSRj7/MmQWZFUjKjrELLjXh2 ss7VBckv5docZ4mH1BYoGHJvcchaWNM7ye6+/yL9L81amee8iPKWm7RCI59yTF7H/ukb vL+NQZ3AGtgp7GSGxNjy15AApb8hJbhunPymCYbkpTWL002Wyki4Ee/SNgSHMhT90A3E 0TCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:ironport-sdr:ironport-sdr; bh=uxAxKETNFfGt04S8Te6nd8jOKcE/92+WZyHsLC8Go8s=; b=hLztpFqQqJkGANr8Z6h3iiwTMSSC09tgWlTGHP5QOqtZfWYyo8O8LqfIWLv6yFGuRB Nt/t/Vwos+2zqRjEkMIWaXS6rLAxnLmkNlcx5hqFwIwV7HBj5nujajUDYwbNkI2I/cUg LRs/A9Fa5kXjRVH0MSJE7knT66FOinzPiAC8i9FX+VzwBOVsEAuPwLLsl6w2O4h9QSkJ dMniwdTIQNxo9SKitwtvItAMhphMdTQj3J/9m++QntLP4PAojdtp3meLjSTGHlH1PsG8 v4nviwI1W5bHU6evxWaEGr1WxPfir6H4pa/9P1C860kWcDeFPDOjt7Er5BNHudafjFg2 jLfw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h4si651790ejj.390.2020.11.05.00.19.22; Thu, 05 Nov 2020 00:19:45 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730224AbgKEIPz (ORCPT + 99 others); Thu, 5 Nov 2020 03:15:55 -0500 Received: from mga04.intel.com ([192.55.52.120]:39805 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730126AbgKEIPx (ORCPT ); Thu, 5 Nov 2020 03:15:53 -0500 IronPort-SDR: 0pRtLZMx62orJVVWj++mThYAe9TPKTWgHS8NRQERuNcR2dalu8ITZLH823HedZPcqFinZ7qwGg Xd50dRkDucTw== X-IronPort-AV: E=McAfee;i="6000,8403,9795"; a="166755719" X-IronPort-AV: E=Sophos;i="5.77,453,1596524400"; d="scan'208";a="166755719" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2020 00:15:53 -0800 IronPort-SDR: inp/WlUKIgOZgD0RspSds4o5QRKCjulq1Y1PJvNpueDjV5CEEmua0DmDtV4uE6wPegjC8GIJhl Nat0UVLNX92g== X-IronPort-AV: E=Sophos;i="5.77,453,1596524400"; d="scan'208";a="539281452" Received: from chenyi-pc.sh.intel.com ([10.239.159.72]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2020 00:15:50 -0800 From: Chenyi Qiang To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Xiaoyao Li Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v3 4/7] KVM: MMU: Refactor pkr_mask to cache condition Date: Thu, 5 Nov 2020 16:18:01 +0800 Message-Id: <20201105081805.5674-5-chenyi.qiang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201105081805.5674-1-chenyi.qiang@intel.com> References: <20201105081805.5674-1-chenyi.qiang@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pkr_mask bitmap indicates if protection key checks are needed for user pages currently. It is indexed by page fault error code bits [4:1] with PFEC.RSVD replaced by the ACC_USER_MASK from the page tables. Refactor it by reverting to the use of PFEC.RSVD. After that, PKS and PKU can share the same bitmap. Signed-off-by: Chenyi Qiang --- arch/x86/kvm/mmu.h | 10 ++++++---- arch/x86/kvm/mmu/mmu.c | 16 ++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index a77bd20c83f9..8f05f7c0f6df 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -199,11 +199,13 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, * index of the protection domain, so pte_pkey * 2 is * is the index of the first bit for the domain. */ - pkr_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3; + if (pte_access & PT_USER_MASK) + pkr_bits = (vcpu->arch.pkru >> (pte_pkey * 2)) & 3; + else + pkr_bits = 0; - /* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ - offset = (pfec & ~1) + - ((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT)); + /* clear present bit */ + offset = (pfec & ~1); pkr_bits &= mmu->pkr_mask >> offset; errcode |= -pkr_bits & PFERR_PK_MASK; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index d22c0813e4b9..39afa865dc1a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4322,21 +4322,25 @@ static void update_pkr_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) { unsigned pfec, pkey_bits; - bool check_pkey, check_write, ff, uf, wf, pte_user; + bool check_pkey, check_write, ff, uf, wf, rsvdf; pfec = bit << 1; ff = pfec & PFERR_FETCH_MASK; uf = pfec & PFERR_USER_MASK; wf = pfec & PFERR_WRITE_MASK; - /* PFEC.RSVD is replaced by ACC_USER_MASK. */ - pte_user = pfec & PFERR_RSVD_MASK; + /* + * PFERR_RSVD_MASK bit is not set if the + * access is subject to PK restrictions. + */ + rsvdf = pfec & PFERR_RSVD_MASK; /* - * Only need to check the access which is not an - * instruction fetch and is to a user page. + * need to check the access which is not an + * instruction fetch and is not a rsvd fault. */ - check_pkey = (!ff && pte_user); + check_pkey = (!ff && !rsvdf); + /* * write access is controlled by PKRU if it is a * user access or CR0.WP = 1. -- 2.17.1