From: Jim Mattson <jmattson@google.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Aaron Lewis <aaronlewis@google.com>,
kvm@vger.kernel.org, pbonzini@redhat.com
Subject: Re: [kvm-unit-tests PATCH] x86: Look up the PTE rather than assuming it
Date: Fri, 29 Oct 2021 15:59:56 -0700 [thread overview]
Message-ID: <CALMp9eSKVn2233GzCtWMEHmeN-WVCot2X44v1PSPEiZp8bH7hw@mail.gmail.com> (raw)
In-Reply-To: <YXx6K9l2QTwbLYng@google.com>
On Fri, Oct 29, 2021 at 3:48 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Fri, Oct 29, 2021, Aaron Lewis wrote:
> > Rather than assuming which PTE the SMEP test is running on, look it up
> > to ensure we have the correct entry. If this test were to run on a
> > different page table (ie: run in an L2 test) the wrong PTE would be set.
> > Switch to looking up the PTE to avoid this from happening.
> >
> > Signed-off-by: Aaron Lewis <aaronlewis@google.com>
> > ---
> > x86/access.c | 9 ++++++---
> > x86/cstart64.S | 1 -
> > 2 files changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/x86/access.c b/x86/access.c
> > index 4725bbd..a4d72d9 100644
> > --- a/x86/access.c
> > +++ b/x86/access.c
> > @@ -204,7 +204,7 @@ static void set_cr0_wp(int wp)
> > static unsigned set_cr4_smep(int smep)
> > {
> > unsigned long cr4 = shadow_cr4;
> > - extern u64 ptl2[];
> > + pteval_t *pte;
> > unsigned r;
> >
> > cr4 &= ~CR4_SMEP_MASK;
> > @@ -213,11 +213,14 @@ static unsigned set_cr4_smep(int smep)
> > if (cr4 == shadow_cr4)
> > return 0;
> >
> > + pte = get_pte(phys_to_virt(read_cr3()), set_cr4_smep);
>
> What guarantees are there that set_cr4_smep() and the rest of the test are mapped
> by the same PTE? I 100% agree the current code is ugly, e.g. I can't remember how
> ptl2[2] is guaranteed to be used, but if we're going to fix it then we should aim
> for a more robust solution.
One possible solution is to put labels around the code that has to run
with SMEP and then to implement a function to modify the PTEs for a
range of addresses, using gcc's double-ampersand syntax to get the
addresses of the labels.
> > + assert(pte);
> > +
> > if (smep)
> > - ptl2[2] &= ~PT_USER_MASK;
> > + *pte &= ~PT_USER_MASK;
> > r = write_cr4_checking(cr4);
> > if (r || !smep) {
> > - ptl2[2] |= PT_USER_MASK;
> > + *pte |= PT_USER_MASK;
> >
> > /* Flush to avoid spurious #PF */
> > invlpg((void *)(2 << 21));
>
> This invlpg() should be updated as well.
>
> > diff --git a/x86/cstart64.S b/x86/cstart64.S
> > index 5c6ad38..4ba9943 100644
> > --- a/x86/cstart64.S
> > +++ b/x86/cstart64.S
> > @@ -26,7 +26,6 @@ ring0stacktop:
> > .data
> >
> > .align 4096
> > -.globl ptl2
> > ptl2:
> > i = 0
> > .rept 512 * 4
> > --
> > 2.33.1.1089.g2158813163f-goog
> >
next prev parent reply other threads:[~2021-10-29 23:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-29 21:48 [kvm-unit-tests PATCH] x86: Look up the PTE rather than assuming it Aaron Lewis
2021-10-29 22:48 ` Sean Christopherson
2021-10-29 22:59 ` Jim Mattson [this message]
2021-11-01 15:00 ` Aaron Lewis
2021-11-01 17:10 ` Jim Mattson
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=CALMp9eSKVn2233GzCtWMEHmeN-WVCot2X44v1PSPEiZp8bH7hw@mail.gmail.com \
--to=jmattson@google.com \
--cc=aaronlewis@google.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).