From: Demi Marie Obenour <demi@invisiblethingslab.com>
To: xen-devel@lists.xenproject.org
Cc: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>,
"Jan Beulich" <jbeulich@suse.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger.pau@citrix.com>, "Wei Liu" <wl@xen.org>,
"Jun Nakajima" <jun.nakajima@intel.com>,
"Kevin Tian" <kevin.tian@intel.com>,
"George Dunlap" <george.dunlap@citrix.com>,
"Tim Deegan" <tim@xen.org>
Subject: Re: [PATCH v4 02/10] x86/mm: Avoid hard-coding PAT in get_page_from_l1e()
Date: Thu, 15 Dec 2022 21:25:07 -0500 [thread overview]
Message-ID: <Y5vXBTH3x8ugZtgn@itl-email> (raw)
In-Reply-To: <8f0a2f4352bf6241e66f2fea1776d0c82a3c566d.1671139149.git.demi@invisiblethingslab.com>
[-- Attachment #1: Type: text/plain, Size: 3086 bytes --]
On Thu, Dec 15, 2022 at 06:57:44PM -0500, Demi Marie Obenour wrote:
> get_page_from_l1e() relied on Xen's choice of PAT, which is brittle in
> the face of future PAT changes. Instead, compute the actual cacheability
> used by the CPU and switch on that, as this will work no matter what PAT
> Xen uses.
>
> No functional change intended.
>
> Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
> ---
> Changes since v3:
> - Compute and use the actual cacheability as seen by the processor.
>
> Changes since v2:
> - Improve commit message.
> ---
> xen/arch/x86/include/asm/processor.h | 8 ++++++++
> xen/arch/x86/mm.c | 19 +++++++++++++------
> 2 files changed, 21 insertions(+), 6 deletions(-)
>
> diff --git a/xen/arch/x86/include/asm/processor.h b/xen/arch/x86/include/asm/processor.h
> index 8e2816fae9b97bd4e153a30cc3802971fe0355af..c3205ed9e10c1a57d23c5ecead66bebd82d87d06 100644
> --- a/xen/arch/x86/include/asm/processor.h
> +++ b/xen/arch/x86/include/asm/processor.h
> @@ -100,6 +100,14 @@
>
> #ifndef __ASSEMBLY__
>
> +/* Convert from PAT/PCD/PWT embedded in PTE flags to actual cacheability value */
> +static inline unsigned int pte_flags_to_cacheability(unsigned int flags)
> +{
> + unsigned int pat_shift = ((flags & _PAGE_PAT) >> 2) |
> + (flags & (_PAGE_PCD|_PAGE_PWT));
This could also be written as
unsigned int pat_shift = pte_flags_to_cacheattr(flags) << 3;
which might be easiser to read.
> + return 0xFF & (XEN_MSR_PAT >> pat_shift);
> +}
> +
> struct domain;
> struct vcpu;
>
> diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
> index 78b1972e4170cacccc9c37c6e64e76e66a7da87f..802073a01c5cf4dc3cf1d58d28ea4d4e9e8149c7 100644
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -959,15 +959,22 @@ get_page_from_l1e(
> flip = _PAGE_RW;
> }
>
> - switch ( l1f & PAGE_CACHE_ATTRS )
> + /* Force cacheable memtypes to UC */
> + switch ( pte_flags_to_cacheability(l1f) )
> {
> - case 0: /* WB */
> - flip |= _PAGE_PWT | _PAGE_PCD;
> + case X86_MT_UC:
> + case X86_MT_UCM:
> + case X86_MT_WC:
> + /* not cached */
> break;
> - case _PAGE_PWT: /* WT */
> - case _PAGE_PWT | _PAGE_PAT: /* WP */
> - flip |= _PAGE_PCD | (l1f & _PAGE_PAT);
> + case X86_MT_WB:
> + case X86_MT_WT:
> + case X86_MT_WP:
> + /* cacheable, force to UC */
> + flip |= (l1f & PAGE_CACHE_ATTRS) ^ _PAGE_UC;
> break;
> + default:
> + BUG();
Question for future reference: is this an appropriate use of BUG()? The
default case is clearly unreachable, and I actually considered using
unreachable() here, but this isn’t performance-critical code from what I
can tell. If execution *does* reach here something is seriously wrong.
--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-12-16 2:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-15 23:57 [PATCH v4 00/10] Make PAT handling less brittle Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 01/10] x86: Add memory type constants Demi Marie Obenour
2022-12-16 7:42 ` Jan Beulich
2022-12-15 23:57 ` [PATCH v4 02/10] x86/mm: Avoid hard-coding PAT in get_page_from_l1e() Demi Marie Obenour
2022-12-16 2:25 ` Demi Marie Obenour [this message]
2022-12-16 7:46 ` Jan Beulich
2022-12-16 2:49 ` Andrew Cooper
2022-12-16 15:21 ` Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 03/10] x86: Replace PAT_* with X86_MT_* Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 04/10] x86: Replace MTRR_* constants with X86_MT_* constants Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 05/10] x86: Replace EPT_EMT_* constants with X86_MT_* Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 06/10] x86: Remove MEMORY_NUM_TYPES and NO_HARDCODE_MEM_TYPE Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 07/10] x86: Derive XEN_MSR_PAT from its individual entries Demi Marie Obenour
2022-12-16 7:52 ` Jan Beulich
2022-12-15 23:57 ` [PATCH v4 08/10] x86/mm: make code robust to future PAT changes Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 09/10] x86/mm: Reject invalid cacheability in PV guests by default Demi Marie Obenour
2022-12-15 23:57 ` [PATCH v4 10/10] x86: Use Linux's PAT Demi Marie Obenour
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=Y5vXBTH3x8ugZtgn@itl-email \
--to=demi@invisiblethingslab.com \
--cc=andrew.cooper3@citrix.com \
--cc=george.dunlap@citrix.com \
--cc=jbeulich@suse.com \
--cc=jun.nakajima@intel.com \
--cc=kevin.tian@intel.com \
--cc=marmarek@invisiblethingslab.com \
--cc=roger.pau@citrix.com \
--cc=tim@xen.org \
--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.