Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1719103

Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU

Path csiph.com!news.redatomik.org!aioe.org!bofh.it!news.nic.it!robomod
From Yang Zhang <yang.zhang.wz@gmail.com>
Newsgroups linux.kernel
Subject Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU
Date Thu, 24 Aug 2017 12:10:01 +0200
Message-ID <uhXfj-52T-1@gated-at.bofh.it> (permalink)
References <uhLnP-5N6-5@gated-at.bofh.it> <uhLnP-5N6-3@gated-at.bofh.it> <uhWjg-4uA-5@gated-at.bofh.it> <uhWsW-4xQ-23@gated-at.bofh.it>
X-Original-To Paolo Bonzini <pbonzini@redhat.com>, linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=nEAnW3arpHtoNVe9nPzWbU99AOCvn541t8ueW3oZH3c=; b=bsv7F/1fc1YCVEW+TZ9QEvxLKHIGK5hy4IgfdJkpup4XeXT1YlvOW4trs8dE5+LxUv TViDfxODvZ6HVrBfjN8GX9tGQiMpLgCJryVgcxcldDy0eN5pEa5zFcQM/l8stOR68z5L 016edwT+tdlBJiNTCoKVqNvy872A3nsKhY+VIWQAIlfIksFw03/vZVcL2MNR6ZhS1Vmh cIt6nLfFcExhVCiaI/HARbRq0y4yEQ4nfUnwgKBrE/7yTocBSV4vjTF5d/dlhB4l6LiR qMzNKFys9RCdCqxoNDXVVhUsROFugBEuircnOkAkfI9LDa6lQA8eEH56SxfUndewN7V0 5cCQ==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=nEAnW3arpHtoNVe9nPzWbU99AOCvn541t8ueW3oZH3c=; b=gNawe3cpDhUvOu8N79/0ZPaktnZ3+noDw2ywP0ycXLxAu0672/e7JZuyhwITf4hM3y B8bRAgznfTj9xHYI+G3IgmjoLroadYYr/A3zXlj4Z5YAeGC2QGhDX1x1JNgj4c5LYMfn gMDChxqoD8BZxgogOXEo9t7CttsTRcPoXidFsoDxtFiMzchcMkk864kGCuEY39lHYN/g j/+EGeZ6hfh9CpUaXCF9fQwItbz/jTlM60XQWYP1gDzPL0Q4oOwprrbjGKGL4z1FyVlJ lCmduCFHqZrdZC/4Z8SY8fw0h9L5jomAwtGCFvFYr9SF+E0jLGFnRRV9upRvdmVCzyeO NMNA==
X-Gm-Message-State AHYfb5i+VvWewfBKvdElLoJbZqL4FtTa3Ai7HaJ95h3HKEHLQRbA2e2Q lvUXhIEMdwQcXQ==
X-Received by 10.202.230.4 with SMTP id d4mr7567597oih.148.1503569164332; Thu, 24 Aug 2017 03:06:04 -0700 (PDT)
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0
MIME-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Language en-US
Content-Transfer-Encoding 7bit
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 76
Organization linux.* mail to news gateway
X-Original-Cc junkang.fjk@alibaba-inc.com
X-Original-Date Thu, 24 Aug 2017 18:05:59 +0800
X-Original-Message-ID <59e6c0e2-6422-7803-5a0f-b3c2b00edb26@gmail.com>
X-Original-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> <40adf946-79ad-87cd-8bfd-6db4dfdbefc3@redhat.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1719103

Show key headers only | View raw


On 2017/8/24 17:19, Paolo Bonzini wrote:
> 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!

ok, since most OS distributions don't support protection key so far 
which means vcpu->arch.pkru always 0 in it and i remember host_pkru will 
be set to 55555554 be default. To avoid the unnecessary access to pkru, 
how about the following change:

@@ -4338,6 +4331,9 @@ static int vmx_set_cr4(struct kvm_vcpu *vcpu, 
unsigned long cr4)
                         return 1;
         }

+       if (cr4 & X86_CR4_PKE)
+               to_vmx(vcpu)->guest_pkru_valid = true;
+
         if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
                 return 1;

@@ -9020,8 +9016,10 @@ static void __noclone vmx_vcpu_run(struct 
kvm_vcpu *vcpu)
         if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
                 vmx_set_interrupt_shadow(vcpu, 0);

-       if (vmx->guest_pkru_valid)
-               __write_pkru(vmx->guest_pkru);
+       if (static_cpu_has(X86_FEATURE_OSPKE) &&
+           vmx->guest_pkru_valid &&
+           vcpu->arch.pkru != vmx->host_pkru)
+               __write_pkru(vcpu->arch.pkru);

         atomic_switch_perf_msrs(vmx);
         debugctlmsr = get_debugctlmsr();
@@ -9169,13 +9167,11 @@ static void __noclone vmx_vcpu_run(struct 
kvm_vcpu *vcpu)
          * back on host, so it is safe to read guest PKRU from current
          * XSAVE.
          */
-       if (boot_cpu_has(X86_FEATURE_OSPKE)) {
-               vmx->guest_pkru = __read_pkru();
-               if (vmx->guest_pkru != vmx->host_pkru) {
-                       vmx->guest_pkru_valid = true;
+       if (static_cpu_has(X86_FEATURE_OSPKE) &&
+           vmx->guest_pkru_valid) {
+               vcpu->arch.pkru = __read_pkru();
+               if (vcpu->arch.pkru != vmx->host_pkru)
                         __write_pkru(vmx->host_pkru);
-               } else
-                       vmx->guest_pkru_valid = false;
         }

         /*

-- 
Yang
Alibaba Cloud Computing

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 1/2] KVM: x86: simplify handling of PKRU Paolo Bonzini <pbonzini@redhat.com> - 2017-08-23 23:30 +0200
  Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU Yang Zhang <yang.zhang.wz@gmail.com> - 2017-08-24 11:10 +0200
    Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU Paolo Bonzini <pbonzini@redhat.com> - 2017-08-24 11:20 +0200
      Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU Yang Zhang <yang.zhang.wz@gmail.com> - 2017-08-24 12:10 +0200
        Re: [PATCH 1/2] KVM: x86: simplify handling of PKRU Paolo Bonzini <pbonzini@redhat.com> - 2017-08-24 12:20 +0200

csiph-web