Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp4014753ioo; Wed, 25 May 2022 12:53:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyM/l4KBpmmEuE2YCqvelN+HWRmT40mAmzVX0A/TdQhkrjt0Gk7IZQOl2+euRNrCaR1f1dI X-Received: by 2002:a17:906:6a27:b0:6fe:d445:e115 with SMTP id qw39-20020a1709066a2700b006fed445e115mr16232681ejc.379.1653508427576; Wed, 25 May 2022 12:53:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653508427; cv=none; d=google.com; s=arc-20160816; b=nGYsSc/sN7f2qfXfFqlbaT9eqPFdKlvWIjH5q0OgSQVfFqTZrnvymTca2wb8QaIcvk dq8NyK9XSrtvSIC8v3L6c/ZIav5mdjia44bKS3A1r73xGCKSjqIswAB2nh8XgLVFCMdY 2KrJdtPw4h0gFbT36gctvSqar0PQW731Xa9I8y5PhtlZNFKi/4LGYHmjFga7pO0WJp+i wNZKHwZcIbD+atBibz2UHk5sYFPVM12PnuvaLxIs9uL/zlm2q5VsIEk96iRu2KVi4q86 bbLAI0RXMHcrFoO5PPqo7jDZJMzuWDPsxS4yb7TFGRxQCI5ufZGSYdqmF+avenEudotY MFBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=M6ibtN+tkzyjMGp3wPL66xycMPufdrhzmfDOY7iX60k=; b=yrkQFn1HM1cwopDSDWDsOVFKYmpJu4qwpbbD/Smf6OcQ4WG9sNldJ146ubaPr7tq2J YnrfsKot8F6pzHioKvlE8Ycya/IyTVNm2x59EWwgSO3QvwYJGsq19SklvW2mWS/mYnJX ID+fRqiISGOAzQ5q03kPZEjoBJATxLjAkGPHmRqowya9AzOEbGoWsQiEYn2wfZ6KxZLd os9VNKpzuhyxNCBHfmbxwUHZtfE+uEOBNK6r6zUYmVIafz8ypZULgVNYI2Cgd6RgwqEx 9UzVBKF20jcU0tRElBRp7g7/YEpZTavgAjn80iV5yCXFfed2KY/sVbdQCy0/Z9ada7lA cAkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kt22Jdr9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cr21-20020a170906d55500b006f3cfd2c84esi22521370ejc.298.2022.05.25.12.53.20; Wed, 25 May 2022 12:53:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kt22Jdr9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242611AbiEXXWG (ORCPT + 99 others); Tue, 24 May 2022 19:22:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242568AbiEXXWD (ORCPT ); Tue, 24 May 2022 19:22:03 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DD342BB1D for ; Tue, 24 May 2022 16:21:58 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id m1so17124506plx.3 for ; Tue, 24 May 2022 16:21:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=M6ibtN+tkzyjMGp3wPL66xycMPufdrhzmfDOY7iX60k=; b=kt22Jdr99111DhfjKgHjr/LHKxyH1S9/yFGXXUNma4i2GW79ZUv6vwgVgBAz29Uu8Q /uT1GYJittlWhqAVzwcZUwbEQCDnBMYiIOqWEHcPRwYmwM8UR3IuU0F/qwrHrPHqa5IG C6/cjEnR4wioV4Wq23KaERGCaQYWw6AT5PmB0zDdNMz/clJa8Da69sanaGPJXQCIe3gY HvQNN3E6kk9vrXwJvhLy73lgrBVCG6vk4671pBVvhjAHp23QNuzQABgyVDDZFC51hvqw 9NPsG0Nlfa5ElmGRIAyEZOgQ7SbcPGivBPa1DFeFzVDjvxNYDNVJduIubjjaYgPwo5pp 8kUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=M6ibtN+tkzyjMGp3wPL66xycMPufdrhzmfDOY7iX60k=; b=cBAzPtrUJIuwc+6P395zmDbzXpDHZvy/ryx5uAPTaXEI0Gm4fGTZOjN5VUGCnUf2X4 0SGTuMIDTmUdmaiHWyJcnoU9wg/65+NSwuBU3kFo0MT5M0k+M5kAEgKQvbIC3zi5+VBi rZSDY7DobOTkU6TMesQarNV87IUcva9w2MHCOZjMkvxl9iyTSF7ANtJq4lnZAIbhPD5V l+wgPddIQjqEkxAmLxVUF7nxDl3xq07gWZhEWO3NZfRdnikmSLwNCepvtqhnYaOv5xwD rSIVUYFdcxCD/dOUW2YTI62xWHNLa129c4TwLJIm5bj8wQE724S0Bu2raJQNKbNIDw57 X9NA== X-Gm-Message-State: AOAM533DPLyovsyYx2HoQyuPShCo1YKttUBhI2oCAAwMo/ZiuQ2p21yk 2WYFwJ0HajxTjQbBNmUQRjicWg== X-Received: by 2002:a17:90b:1b03:b0:1dc:a80b:8004 with SMTP id nu3-20020a17090b1b0300b001dca80b8004mr7044789pjb.182.1653434517536; Tue, 24 May 2022 16:21:57 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id i5-20020a170902eb4500b0015e8d4eb248sm4819049pli.146.2022.05.24.16.21.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 16:21:56 -0700 (PDT) Date: Tue, 24 May 2022 23:21:53 +0000 From: Sean Christopherson To: Lei Wang Cc: pbonzini@redhat.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, chenyi.qiang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 5/8] KVM: MMU: Add helper function to get pkr bits Message-ID: References: <20220424101557.134102-1-lei4.wang@intel.com> <20220424101557.134102-6-lei4.wang@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220424101557.134102-6-lei4.wang@intel.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Apr 24, 2022, Lei Wang wrote: > Extra the PKR stuff to a separate, non-inline helper, which is a s/Extra/Extract > preparation to introduce pks support. Please provide more justification. The change is justified, by random readers of this patch/commit will be clueless. Extract getting the effective PKR bits to a helper that lives in mmu.c in order to keep the is_cr4_*() helpers contained to mmu.c. Support for PKRS (versus just PKRU) will require querying MMU state to see if the relevant CR4 bit is enabled because pkr_mask will be non-zero if _either_ bit is enabled). PKR{U,S} are exposed to the guest if and only if TDP is enabled, and while permission_fault() is performance critical for ia32 shadow paging, it's a rarely used path with TDP is enabled. I.e. moving the PKR code out-of-line is not a performance concern. > Signed-off-by: Lei Wang > --- > arch/x86/kvm/mmu.h | 20 +++++--------------- > arch/x86/kvm/mmu/mmu.c | 21 +++++++++++++++++++++ > 2 files changed, 26 insertions(+), 15 deletions(-) > > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h > index cb3f07e63778..cea03053a153 100644 > --- a/arch/x86/kvm/mmu.h > +++ b/arch/x86/kvm/mmu.h > @@ -204,6 +204,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, > return vcpu->arch.mmu->page_fault(vcpu, &fault); > } > > +u32 kvm_mmu_pkr_bits(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, kvm_mmu_get_pkr_bits() so that there's a verb in there. > + unsigned pte_access, unsigned pte_pkey, unsigned int pfec); > + > /* > * Check if a given access (described through the I/D, W/R and U/S bits of a > * page fault error code pfec) causes a permission fault with the given PTE > @@ -240,21 +243,8 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, > > WARN_ON(pfec & (PFERR_PK_MASK | PFERR_RSVD_MASK)); > if (unlikely(mmu->pkr_mask)) { > - u32 pkr_bits, offset; > - > - /* > - * PKRU defines 32 bits, there are 16 domains and 2 > - * attribute bits per domain in pkru. pte_pkey is the > - * 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; > - > - /* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ > - offset = (pfec & ~1) + > - ((pte_access & PT_USER_MASK) << (PFERR_RSVD_BIT - PT_USER_SHIFT)); > - > - pkr_bits &= mmu->pkr_mask >> offset; > + u32 pkr_bits = > + kvm_mmu_pkr_bits(vcpu, mmu, pte_access, pte_pkey, pfec); Nit, I prefer wrapping in the params, that way the first line shows the most important information, e.g. what variable is being set and how (by a function call). And then there won't be overflow with the longer helper name: u32 pkr_bits = kvm_mmu_get_pkr_bits(vcpu, mmu, pte_access, pte_pkey, pfec); > errcode |= -pkr_bits & PFERR_PK_MASK; > fault |= (pkr_bits != 0); > } > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index de665361548d..6d3276986102 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -6477,3 +6477,24 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm) > if (kvm->arch.nx_lpage_recovery_thread) > kthread_stop(kvm->arch.nx_lpage_recovery_thread); > } > + > +u32 kvm_mmu_pkr_bits(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, > + unsigned pte_access, unsigned pte_pkey, unsigned int pfec) > +{ > + u32 pkr_bits, offset; > + > + /* > + * PKRU defines 32 bits, there are 16 domains and 2 Comment needs to be aligned, and it can be adjust to wrap at 80 chars (its indentation has changed). /* * PKRU and PKRS both define 32 bits. There are 16 domains and 2 * attribute bits per domain in them. pte_key is the index of the * protection domain, so pte_pkey * 2 is the index of the first bit for * the domain. The use of PKRU versus PKRS is selected by the address * type, as determined by the U/S bit in the paging-structure entries. */ > + * attribute bits per domain in pkru. pte_pkey is the > + * 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; > + > + /* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ > + offset = (pfec & ~1) + ((pte_access & PT_USER_MASK) > + << (PFERR_RSVD_BIT - PT_USER_SHIFT)); > + > + pkr_bits &= mmu->pkr_mask >> offset; > + return pkr_bits; > +} > -- > 2.25.1 >