All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <Andrew.Cooper3@citrix.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: Roger Pau Monne <roger.pau@citrix.com>, Wei Liu <wl@xen.org>,
	Kevin Tian <kevin.tian@intel.com>,
	Xen-devel <xen-devel@lists.xenproject.org>
Subject: Re: [PATCH 3/6] x86/hvm: Context switch MSR_PKRS
Date: Mon, 9 Jan 2023 16:55:19 +0000	[thread overview]
Message-ID: <83a08e78-cbee-8486-479a-5255c42e8239@citrix.com> (raw)
In-Reply-To: <6efed2d5-e7a0-139a-a2b3-6f0696f711d1@suse.com>

On 21/12/2021 11:56 am, Jan Beulich wrote:
> On 16.12.2021 10:54, Andrew Cooper wrote:
>> @@ -42,4 +45,49 @@ static inline void wrpkru(uint32_t pkru)
>>                     :: "a" (pkru), "d" (0), "c" (0) );
>>  }
>>  
>> +/*
>> + * Xen does not use PKS.
>> + *
>> + * Guest kernel use is expected to be one default key, except for tiny windows
>> + * with a double write to switch to a non-default key in a permitted critical
>> + * section.
>> + *
>> + * As such, we want MSR_PKRS un-intercepted.  Furthermore, as we only need it
>> + * in Xen for emulation or migration purposes (i.e. possibly never in a
>> + * domain's lifetime), we don't want to re-sync the hardware value on every
>> + * vmexit.
>> + *
>> + * Therefore, we read and cache the guest value in ctxt_switch_from(), in the
>> + * expectation that we can short-circuit the write in ctxt_switch_to().
>> + * During regular operations in current context, the guest value is in
>> + * hardware and the per-cpu cache is stale.
>> + */
>> +DECLARE_PER_CPU(uint32_t, pkrs);
>> +
>> +static inline uint32_t rdpkrs(void)
>> +{
>> +    uint32_t pkrs, tmp;
>> +
>> +    rdmsr(MSR_PKRS, pkrs, tmp);
>> +
>> +    return pkrs;
>> +}
>> +
>> +static inline uint32_t rdpkrs_and_cache(void)
>> +{
>> +    return this_cpu(pkrs) = rdpkrs();
>> +}
>> +
>> +static inline void wrpkrs(uint32_t pkrs)
>> +{
>> +    uint32_t *this_pkrs = &this_cpu(pkrs);
>> +
>> +    if ( *this_pkrs != pkrs )
> For this to work, I think we need to clear PKRS during CPU init; I
> admit I didn't peek ahead in the series to check whether you do so
> later on in the series. At least the version of the SDM I'm looking
> at doesn't even specify reset state of 0 for this MSR. But even if
> it did, it would likely be as for PKRU - unchanged after INIT. Yet
> INIT is all that CPUs go through when e.g. parking / unparking them.

While trying to address this, I've noticed that we don't sanitise PKRU
during CPU init either.

This will explode in a fun way if e.g. we kexec into a new Xen, but
leave PKEY 0 with AD/WD, and try building a PV dom0.

As soon as we've fully context switched into a vCPU context, we'll pick
up the 0 from XSTATE and do the right thing by default.

~Andrew

  reply	other threads:[~2023-01-09 16:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-16  9:54 [PATCH 0/6] x86: Support PKS Andrew Cooper
2021-12-16  9:54 ` [PATCH 1/6] x86/prot-key: Enumeration for Protection Key Supervisor Andrew Cooper
2021-12-21 11:15   ` Jan Beulich
2021-12-16  9:54 ` [PATCH 2/6] x86/prot-key: Split PKRU infrastructure out of asm/processor.h Andrew Cooper
2021-12-21 11:28   ` Jan Beulich
2023-01-09 14:57     ` Andrew Cooper
2023-01-09 16:30       ` Jan Beulich
2021-12-16  9:54 ` [PATCH 3/6] x86/hvm: Context switch MSR_PKRS Andrew Cooper
2021-12-21 11:56   ` Jan Beulich
2023-01-09 16:55     ` Andrew Cooper [this message]
2021-12-16  9:54 ` [PATCH 4/6] x86/hvm: Enable guest access to MSR_PKRS Andrew Cooper
2021-12-21 11:58   ` Jan Beulich
2021-12-16  9:54 ` [PATCH 5/6] x86/pagewalk: Support PKS Andrew Cooper
2021-12-21 12:07   ` Jan Beulich
2021-12-16  9:54 ` [PATCH 6/6] x86/hvm: " Andrew Cooper
2021-12-21 12:18   ` Jan Beulich
2023-01-09 17:01     ` Andrew Cooper
2021-12-16 21:09 ` [PATCH 0/6] x86: " Andrew Cooper

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=83a08e78-cbee-8486-479a-5255c42e8239@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=kevin.tian@intel.com \
    --cc=roger.pau@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.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.