From: Paolo Bonzini <pbonzini@redhat.com>
To: Xiao Guangrong <guangrong.xiao@linux.intel.com>,
Huaitong Han <huaitong.han@intel.com>,
gleb@kernel.org
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH V4 3/7] KVM, pkeys: update memeory permission bitmask for pkeys
Date: Tue, 8 Mar 2016 11:01:30 +0100 [thread overview]
Message-ID: <56DEA2FA.70302@redhat.com> (raw)
In-Reply-To: <56DE991E.8070007@linux.intel.com>
On 08/03/2016 10:19, Xiao Guangrong wrote:
>>>
>>> As PKEY is not enabled on softmmu, the gva_to_gpa mostly comes from
>>> internal
>>> KVM, that means we should always set PFEC.PKEY for all the gva_to_gpa
>>> request.
>>> Wasting a bit is really unnecessary.
>>>
>>> And it is always better to move more workload from permission_fault() to
>>> update_permission_bitmask() as the former is much hotter than the
>>> latter.
>>
>> I agree, but I'm not sure why you say that adding a bits adds more work
>> to permission_fault().
>
> A branch to check PFEC.PKEY, which is not well predictable on soft mmu. (It
> should always be set in EPT as the page table walking is done by software,
> however, if we only consider EPT we can assume it is always true).
Ok, I agree we shouldn't add such a branch.
It's really messy that PKERR_PK_MASK is computed at runtime. It means
we need to check U=1 in permission_fault, and we need to rethink all of
the handling of PKERR_PK_MASK in fact. I think we should extend the
algorithm from earlier in the thread to handle the AD bit too:
/* Bit 0: should PKRU.AD be considered if PFEC=0 or 1?
* Bit 1: should PKRU.WD be considered if PFEC=0 or 1?
* Bit 2: should PKRU.AD be considered if PFEC=2 or 3?
* Bit 3: should PKRU.WD be considered if PFEC=2 or 3?
* Ignore PKRU if U=0.
* etc.
*/
u32 pkru_mask;
In update_permission_bitmask, the bits of mmu->pkru_mask only depend on
the byte, not on the bit.
Bits 0, 2, ... are computed from pku, i.e. cr4_pku && long_mode && !ff.
Bits 1, 3, ... are computed from pku && (wf &&
(is_write_protection(vcpu) || uf)).
The permissions[] array can keep 16 entries, because PFERR_PK_MASK is
never passed to permission_fault (like PFERR_RSVD_MASK). The
permission_fault code is something like:
WARN_ON(pfec & PFERR_PK_MASK);
pkru_mask = pte_access & ACC_USER_MASK ? mmu->pkru_mask : 0;
pkru_bits &= pkru_mask >> (pfec & ~1);
pfec |= -pkru_bits & PFERR_PK_MASK;
What do you think?
I still believe that the above checks should be wrapped in a
if (unlikely(mmu->pkru_mask) {
...
}
It should be predicted well and removes the potential performance cost
of branchless code on !PKE OSes.
>> Adding a bit lets us skip CR4.PKU and EFER.LMA checks in
>> permission_fault() and in all gva_to_gpa() callers.
>
> The point is when we can clear this bit to skip these checks. We should
> _always_ check PKEY even if PFEC.PKEY = 0, because:
> 1) all gva_to_gpa()s issued by KVM should always check PKEY. This is the
> case of ept only.
>
> 2) if the feature is enabled in softmmu, shadow page table may change its
> behavior, for example, the mmio-access causes a reserved PF which
> may clear PFEC.PKEY.
>
> And skipping these checks is not really necessary as we can take them into
> account when we update the bitmask.
Got it. Does the above pseudocode match your idea? It disregards the
pagefault's PFERR_PK_MASK completely.
Paolo
next prev parent reply other threads:[~2016-03-08 10:01 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-05 11:27 [PATCH V4 0/7] KVM, pkeys: add memory protection-key support Huaitong Han
2016-03-05 11:27 ` [PATCH V4 1/7] KVM, pkeys: expose CPUID/CR4 to guest Huaitong Han
2016-03-06 7:15 ` Xiao Guangrong
2016-03-06 23:20 ` Paolo Bonzini
2016-03-08 7:39 ` Xiao Guangrong
2016-03-08 7:58 ` Paolo Bonzini
2016-03-05 11:27 ` [PATCH V4 2/7] KVM, pkeys: disable pkeys for guests in non-paging mode Huaitong Han
2016-03-06 7:19 ` Xiao Guangrong
2016-03-08 12:09 ` Yang Zhang
2016-03-08 12:11 ` Paolo Bonzini
2016-03-08 13:02 ` Yang Zhang
2016-03-05 11:27 ` [PATCH V4 3/7] KVM, pkeys: update memeory permission bitmask for pkeys Huaitong Han
2016-03-06 7:42 ` Xiao Guangrong
2016-03-06 23:14 ` Paolo Bonzini
2016-03-08 7:35 ` Xiao Guangrong
2016-03-08 8:29 ` Paolo Bonzini
2016-03-08 9:19 ` Xiao Guangrong
2016-03-08 10:01 ` Paolo Bonzini [this message]
2016-03-09 5:03 ` Xiao Guangrong
2016-03-09 8:10 ` Paolo Bonzini
2016-03-05 11:27 ` [PATCH V4 4/7] KVM, pkeys: add pkeys support for permission_fault logic Huaitong Han
2016-03-06 8:00 ` Xiao Guangrong
2016-03-06 20:36 ` Paolo Bonzini
2016-03-06 23:29 ` Paolo Bonzini
2016-03-08 5:57 ` Xiao Guangrong
2016-03-05 11:27 ` [PATCH V4 5/7] KVM, pkeys: Add pkeys support for gva_to_gpa funcions Huaitong Han
2016-03-06 8:01 ` Xiao Guangrong
2016-03-06 21:33 ` Paolo Bonzini
2016-03-05 11:27 ` [PATCH V4 6/7] KVM, pkeys: add pkeys support for xsave state Huaitong Han
2016-03-06 8:27 ` Xiao Guangrong
2016-03-05 11:27 ` [PATCH V4 7/7] KVM, pkeys: disable PKU feature without ept Huaitong Han
2016-03-06 9:28 ` Xiao Guangrong
2016-03-06 20:32 ` Paolo Bonzini
2016-03-08 5:54 ` Xiao Guangrong
2016-03-08 8:47 ` Paolo Bonzini
2016-03-08 9:32 ` Xiao Guangrong
2016-03-08 10:02 ` Paolo Bonzini
2016-03-09 5:51 ` Xiao Guangrong
2016-03-09 6:37 ` Yang Zhang
2016-03-09 7:21 ` Xiao Guangrong
2016-03-09 7:41 ` Yang Zhang
2016-03-09 7:50 ` Xiao Guangrong
2016-03-09 8:00 ` Yang Zhang
2016-03-09 8:05 ` Xiao Guangrong
2016-03-09 8:18 ` Paolo Bonzini
2016-03-09 8:13 ` Paolo Bonzini
2016-03-09 6:24 ` Yang Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56DEA2FA.70302@redhat.com \
--to=pbonzini@redhat.com \
--cc=gleb@kernel.org \
--cc=guangrong.xiao@linux.intel.com \
--cc=huaitong.han@intel.com \
--cc=kvm@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.