Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752261AbdHXJTi (ORCPT ); Thu, 24 Aug 2017 05:19:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45010 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752036AbdHXJTg (ORCPT ); Thu, 24 Aug 2017 05:19:36 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8DCDD20274 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=pbonzini@redhat.com Subject: Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU To: Yang Zhang , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: junkang.fjk@alibaba-inc.com References: <1503523566-25624-1-git-send-email-pbonzini@redhat.com> <1503523566-25624-2-git-send-email-pbonzini@redhat.com> <8b7cd59e-05b9-e8c4-b686-8a3fda88c191@gmail.com> From: Paolo Bonzini Message-ID: <40adf946-79ad-87cd-8bfd-6db4dfdbefc3@redhat.com> Date: Thu, 24 Aug 2017 11:19:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <8b7cd59e-05b9-e8c4-b686-8a3fda88c191@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 24 Aug 2017 09:19:36 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 937 Lines: 26 On 24/08/2017 11:09, Yang Zhang wrote: >> + if (static_cpu_has(X86_FEATURE_OSPKE) && > > We expose protection key to VM without check whether OSPKE is enabled or > not. Why not check guest's cpuid here which also can avoid unnecessary > access to pkru? Checking guest CPUID is pretty slow. We could check CR4.PKE though. Also, using static_cpu_has with OSPKE is probably wrong. But if we do check CR4.PKE, we can check X86_FEATURE_PKU instead, so something like if (static_cpu_has(X86_FEATURE_PKU) && kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && vcpu->arch.pkru != vmx->host_pkru) ... but then, kvm_read_cr4_bits is also pretty slow---and we don't really need it, since all CR4 writes cause a vmexit. So for now I'd stay with this patch, only s/static_cpu_has/boot_cpu_has/g. Of course you can send improvements on top! Paolo >> + vcpu->arch.pkru != vmx->host_pkru) >> + __write_pkru(vcpu->arch.pkru);