Make PAT handling less brittle
PATCH v6 1/5 x86/mm: Avoid hard-coding PAT in get_page_from_l1e()
In reply to Demi Marie Obenourget_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. This code is itself questionable and may be removed in the future, but removing it would be an observable behavior change and so is out of scope for this patch series.
---
Changes since v5:
- Make comment in get_page_from_l1e() future-proof.
- Explicitly state how known-uncacheable and potentially-cacheable types are handled.
Changes since v4:
- Do not add new pte_flags_to_cacheability() helper, as this code may be removed in the near future and so adding a new helper for it is a bad idea.
- Do not BUG() in the event of an unexpected cacheability. This cannot happen, but it is simpler to force such types to UC than to prove that the BUG() is not reachable.
Changes since v3:
- Compute and use the actual cacheability as seen by the processor.
Changes since v2:
- Improve commit message.
--- xen/arch/x86/mm.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
xen/arch/x86/mm.c +8 -6
@@ -959,14 +959,16 @@ get_page_from_l1e( flip = _PAGE_RW; } -switch ( l1f &PAGE_CACHE_ATTRS ) +switch (0xFF & (XEN_MSR_PAT >> (8 *pte_flags_to_cacheattr (l1f))) ) { -case 0 :/* WB */ - flip |= _PAGE_PWT | _PAGE_PCD; +case X86_MT_UC : +case X86_MT_UCM : +case X86_MT_WC : +/* not cacheable, allow */ break ; -case _PAGE_PWT:/* WT */ -case _PAGE_PWT | _PAGE_PAT:/* WP */ - flip |= _PAGE_PCD | (l1f & _PAGE_PAT); +default : +/* potentially cacheable, force to UC */ + flip |= ((l1f &PAGE_CACHE_ATTRS ) ^ _PAGE_UC);break ; }
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
v6 cover Make PAT handling less brittle
While working on Qubes OS Marek found out that there were some PAT hacks in the Linux i195 driver. I decided to make Xen use Linux’s PAT to see if it solved the graphics glitches that were observed; it did. This required a substantial amount of preliminary work that is useful even without using Linux’s PAT.
Patches 1 through 4 are the preliminary work and I would like them to be accepted into upstream Xen. Patch 4 does break ABI by rejecting the unused PAT entries, but this will only impact buggy PV guests and can be disabled with a Xen command-line option. Patch 5 actually switches to Linux’s PAT and is NOT intended to be merged (at least for now) as it would at a minimum break migration of PV guests from hosts that do not have the patch.
Only patches 4 and 5 actually change Xen’s observable behavior. Patch 1 is a prerequisite that removes the last place where Xen hard-codes its current PAT value. Patch 2 is strictly cleanup. Patch 3 makes changing the PAT much less error-prone, as problems with the PAT or with the associated _PAGE_* constants will be detected at compile time.
Demi Marie Obenour (5):
x86/mm: Avoid hard-coding PAT in get_page_from_l1e()
x86: Remove MEMORY_NUM_TYPES and NO_HARDCODE_MEM_TYPE x86/mm: make code robust to future PAT changes x86/mm: Reject invalid cacheability in PV guests by default
x86: Use Linux's PAT
docs/misc/xen-command-line.pandoc | 11 ++ xen/arch/x86/hvm/mtrr.c | 18 ++-- xen/arch/x86/include/asm/mtrr.h | 2 - xen/arch/x86/include/asm/page.h | 4 +- xen/arch/x86/include/asm/processor.h | 10 +- xen/arch/x86/mm.c | 146 ++++++++++++++++++++++++--- xen/arch/x86/mm/shadow/multi.c | 2 +- 7 files changed, 162 insertions(+), 31 deletions(-)
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v6 4/5 x86/mm: Reject invalid cacheability in PV guests by default
Setting cacheability flags that are not ones specified by Xen is a bug in the guest. By default, return -EINVAL if a guests attempts to do this. The invalid-cacheability= Xen command-line flag allows the administrator to allow such attempts or to produce
---
Changes since v5:
- Make parameters static and __ro_after_init.
- Replace boolean parameter allow_invalid_cacheability with string parameter invalid-cacheability.
- Move parameter definitions to near where they are used.
- Add documentation.
Changes since v4:
- Remove pointless BUILD_BUG_ON().
- Add comment explaining why an exception is being injected.
Changes since v3:
- Add Andrew Cooper’s Suggested-by
--- docs/misc/xen-command-line.pandoc | 11 ++++++ xen/arch/x86/mm.c | 60 ++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.pandoc +11 -0
@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86) +> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. + ### ioapic_ack (x86) > `= old | new`
xen/arch/x86/mm.c +59 -1
72 lines not on this page · raw message
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v6 2/5 x86: Remove MEMORY_NUM_TYPES and NO_HARDCODE_MEM_TYPE
In reply to Demi Marie ObenourNo functional change intended.
---
Changes since v2:
- Keep MTRR_NUM_TYPES and adjust commit message accordingly
--- xen/arch/x86/hvm/mtrr.c | 18 +++++++++--------- xen/arch/x86/include/asm/mtrr.h | 2 -- xen/arch/x86/mm/shadow/multi.c | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/mtrr.c +9 -9
@@ -38,7 +38,7 @@ static const uint8_t pat_entry_2_pte_flags[8] = {/* Effective mm type lookup table, according to MTRR and PAT. */ static const uint8_t mm_type_tbl[MTRR_NUM_TYPES ][X86_NUM_MT ] = { -#define RS MEMORY_NUM_TYPES +#define RS MTRR_NUM_TYPES #define UC X86_MT_UC #define WB X86_MT_WB #define WC X86_MT_WC
@@ -66,9 +66,9 @@ static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][X86_NUM_MT] = {* Reverse lookup table, to find a pat type according to MTRR and effective * memory type. This table is dynamically generated. */ -static uint8_t __read_mostly mtrr_epat_tbl[MTRR_NUM_TYPES ][MEMORY_NUM_TYPES ] = - { [0 ...MTRR_NUM_TYPES -1 ] = - { [0 ...MEMORY_NUM_TYPES -1 ] =INVALID_MEM_TYPE } +static uint8_t __read_mostly mtrr_epat_tbl[MTRR_NUM_TYPES ][MTRR_NUM_TYPES ] = + { [0 ...MTRR_NUM_TYPES -1 ] = + { [0 ...MTRR_NUM_TYPES -1 ] =INVALID_MEM_TYPE } };/* Lookup table for PAT entry of a given PAT value in host PAT. */
@@ -85,7 +85,7 @@ static int __init cf_check hvm_mtrr_pat_init(void) {unsigned int tmp = mm_type_tbl[i][j]; -if ( tmp <MEMORY_NUM_TYPES ) +if ( tmp <MTRR_NUM_TYPES ) mtrr_epat_tbl[i][tmp] = j; } }
@@ -317,11 +317,11 @@ static uint8_t effective_mm_type(struct mtrr_state *m, uint8_t gmtrr_mtype) {uint8_t mtrr_mtype, pat_value; - +/* if get_pat_flags() gives a dedicated MTRR type, * just use it -*/ -if ( gmtrr_mtype ==NO_HARDCODE_MEM_TYPE ) +*/ +if ( gmtrr_mtype ==MTRR_NUM_TYPES ) mtrr_mtype =mtrr_get_type (m, gpa,0 );else mtrr_mtype = gmtrr_mtype;
@@ -346,7 +346,7 @@ uint32_t get_pat_flags(struct vcpu *v,/* 1. Get the effective memory type of guest physical address, * with the pair of guest MTRR and PAT */ - guest_eff_mm_type =effective_mm_type (g, pat, gpaddr, + guest_eff_mm_type =effective_mm_type (g, pat, gpaddr, gl1e_flags, gmtrr_mtype);/* 2. Get the memory type of host physical address, with MTRR */ shadow_mtrr_type =mtrr_get_type (&mtrr_state, spaddr,0 );
xen/arch/x86/include/asm/mtrr.h +0 -2
@@ -4,8 +4,6 @@#include <xen/mm.h> #define MTRR_NUM_TYPES X86_MT_UCM -#define MEMORY_NUM_TYPES MTRR_NUM_TYPES -#define NO_HARDCODE_MEM_TYPE MTRR_NUM_TYPES#define NORMAL_CACHE_MODE 0#define NO_FILL_CACHE_MODE 2
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -578,7 +578,7 @@ _sh_propagate(struct vcpu *v, gflags,gfn_to_paddr (target_gfn),mfn_to_maddr (target_mfn), - NO_HARDCODE_MEM_TYPE); + MTRR_NUM_TYPES); } }
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v6 3/5 x86/mm: make code robust to future PAT changes
In reply to Demi Marie ObenourIt may be desirable to change Xen's PAT for various reasons. This requires changes to several _PAGE_* macros as well. Add static assertions to check that XEN_MSR_PAT is consistent with the _PAGE_* macros, and that _PAGE_WB is 0 as required by Linux.
---
Changes since v5:
- Remove unhelpful comment.
- Move a BUILD_BUG_ON.
- Use fewer hardcoded constants in PTE_FLAGS_TO_CACHEATTR.
- Fix coding style.
Changes since v4:
- Add lots of comments explaining what the various BUILD_BUG_ON()s mean.
Changes since v3:
- Refactor some macros
- Avoid including a string literal in BUILD_BUG_ON
--- xen/arch/x86/mm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
xen/arch/x86/mm.c +70 -0
@@ -6352,6 +6352,11 @@ unsigned long get_upper_mfn_bound(void)return min (max_mfn,1UL << (paddr_bits -PAGE_SHIFT )) -1 ; } + +/* +* A bunch of static assertions to check that the XEN_MSR_PAT is valid +* and consistent with the _PAGE_* macros, and that _PAGE_WB is zero. +*/ static void __init __maybe_unused build_assertions (void ) {/*
@@ -6361,6 +6366,71 @@ static void __init __maybe_unused build_assertions(void)* using different PATs will not work. */ BUILD_BUG_ON (XEN_MSR_PAT !=0x050100070406ULL ); + +/* +* _PAGE_WB must be zero for several reasons, not least because Linux +* PV guests assume it. +*/ +BUILD_BUG_ON (_PAGE_WB); + +#define PAT_ENTRY (v) \ + (BUILD_BUG_ON_ZERO(((v) < 0) || ((v) > 7)) + \ + (0xFF & (XEN_MSR_PAT >> (8 * (v))))) + +/* Validate at compile-time that v is a valid value for a PAT entry */ +#define CHECK_PAT_ENTRY_VALUE (v) \ + BUILD_BUG_ON((v) < 0 || (v) > 7 || \ + (v) == X86_MT_RSVD_2 || (v) == X86_MT_RSVD_3) + +/* Validate at compile-time that PAT entry v is valid */ +#define CHECK_PAT_ENTRY (v) CHECK_PAT_ENTRY_VALUE(PAT_ENTRY(v)) + +/* +* If one of these trips, the corresponding entry in XEN_MSR_PAT is invalid. +* This would cause Xen to crash (with #GP) at startup. +*/ +CHECK_PAT_ENTRY (0 ); +CHECK_PAT_ENTRY (1 ); +CHECK_PAT_ENTRY (2 ); +CHECK_PAT_ENTRY (3 ); +CHECK_PAT_ENTRY (4 ); +CHECK_PAT_ENTRY (5 ); +CHECK_PAT_ENTRY (6 ); +CHECK_PAT_ENTRY (7 ); + +#undef CHECK_PAT_ENTRY +#undef CHECK_PAT_ENTRY_VALUE + +/* Macro version of pte_flags_to_cacheattr(), for use in BUILD_BUG_ON()s */ +#define PTE_FLAGS_TO_CACHEATTR (pte_value) \ + ((((pte_value) & _PAGE_PAT) >> 5) | \ + (((pte_value) & (_PAGE_PCD | _PAGE_PWT)) >> 3)) + +/* Check that a PAT-related _PAGE_* macro is correct */ +#define CHECK_PAGE_VALUE (page_value) do { \ +/* Check that the _PAGE_* macros only use bits from PAGE_CACHE_ATTRS */ \ + BUILD_BUG_ON(((_PAGE_ ## page_value) & PAGE_CACHE_ATTRS) != \ + (_PAGE_ ## page_value)); \ +/* Check that the _PAGE_* are consistent with XEN_MSR_PAT */ \ + BUILD_BUG_ON(PAT_ENTRY(PTE_FLAGS_TO_CACHEATTR(_PAGE_ ## page_value)) != \ + (X86_MT_ ## page_value)); \ +} while ( false ) + +/* +* If one of these trips, the corresponding _PAGE_* macro is inconsistent +* with XEN_MSR_PAT. This would cause Xen to use incorrect cacheability +* flags, with results that are unknown and possibly harmful. +*/ +CHECK_PAGE_VALUE (WT ); +CHECK_PAGE_VALUE (WB ); +CHECK_PAGE_VALUE (WC ); +CHECK_PAGE_VALUE (UC ); +CHECK_PAGE_VALUE (UCM ); +CHECK_PAGE_VALUE (WP ); + +#undef CHECK_PAGE_VALUE +#undef PAGE_FLAGS_TO_CACHEATTR +#undef PAT_ENTRY }/*
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
PATCH v6 5/5 x86: Use Linux's PAT
In reply to Demi Marie ObenourThis is purely for testing, to see if it works around a bug in i915. It is not intended to be merged.
NOT-signed-off-by: DO NOT MERGE
--- xen/arch/x86/include/asm/page.h | 4 ++-- xen/arch/x86/include/asm/processor.h | 10 +++++----- xen/arch/x86/mm.c | 8 -------- 3 files changed, 7 insertions(+), 15 deletions(-)
xen/arch/x86/include/asm/page.h +2 -2
@@ -333,11 +333,11 @@ void efi_update_l4_pgtable(unsigned int l4idx, l4_pgentry_t);/* Memory types, encoded under Xen's choice of MSR_PAT. */ #define _PAGE_WB ( 0) -#define _PAGE_WT ( _PAGE_PWT) +#define _PAGE_WC ( _PAGE_PWT)#define _PAGE_UCM ( _PAGE_PCD )#define _PAGE_UC ( _PAGE_PCD | _PAGE_PWT) -#define _PAGE_WC (_PAGE_PAT )#define _PAGE_WP (_PAGE_PAT | _PAGE_PWT) +#define _PAGE_WT (_PAGE_PAT | _PAGE_PCD | _PAGE_PWT)/* * Debug option: Ensure that granted mappings are not implicitly unmapped.
xen/arch/x86/include/asm/processor.h +5 -5
@@ -94,16 +94,16 @@/* * Host IA32_CR_PAT value to cover all memory types. This is not the default -* MSR_PAT value, and is an ABI with PV guests. +* MSR_PAT value, and is needed by the Linux i915 driver. */ #define XEN_MSR_PAT ((_AC(X86_MT_WB, ULL) << 0x00) | \ - (_AC(X86_MT_WT, ULL) << 0x08) | \ + (_AC(X86_MT_WC, ULL) << 0x08) | \ (_AC(X86_MT_UCM, ULL) << 0x10) | \ (_AC(X86_MT_UC, ULL) << 0x18) | \ - (_AC(X86_MT_WC, ULL) << 0x20) | \ + (_AC(X86_MT_WB, ULL) << 0x20) | \ (_AC(X86_MT_WP, ULL) << 0x28) | \ - (_AC(X86_MT_UC, ULL) << 0x30) | \ - (_AC(X86_MT_UC, ULL) << 0x38)) + (_AC(X86_MT_UCM, ULL) << 0x30) | \ + (_AC(X86_MT_WT, ULL) << 0x38))#ifndef __ASSEMBLY__
xen/arch/x86/mm.c +0 -8
@@ -6417,14 +6417,6 @@ unsigned long get_upper_mfn_bound(void)*/ static void __init __maybe_unused build_assertions (void ) { -/* -* If this trips, any guests that blindly rely on the public API in xen.h -* (instead of reading the PAT from Xen, as Linux 3.19+ does) will be -* broken. Furthermore, live migration of PV guests between Xen versions -* using different PATs will not work. -*/ -BUILD_BUG_ON (XEN_MSR_PAT !=0x050100070406ULL ); -/* * _PAGE_WB must be zero for several reasons, not least because Linux * PV guests assume it.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Re: [PATCH v6 3/5] x86/mm: make code robust to future PAT changes
In reply to Demi Marie Obenourxen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6356
Quoted 8 lines · Demi Marie Obenour
xen/arch/x86/mm.c +2
-0@@ -6352,6 +6352,11 @@ unsigned long get_upper_mfn_bound(void)return min (max_mfn,1UL << (paddr_bits -PAGE_SHIFT )) -1 ; } + +/*
Nit: Please avoid introducing double blank lines.
xen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6358
Quoted 2 lines
xen/arch/x86/mm.c +2
-0+* A bunch of static assertions to check that the XEN_MSR_PAT is valid +* and consistent with the _PAGE_* macros, and that _PAGE_WB is zero.
This comment is too specific for a function of ...
xen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6360
Quoted 2 lines
xen/arch/x86/mm.c +1
-0+*/ static void __init __maybe_unused build_assertions (void )
... this name, in this file.
Quoted 8 lines
xen/arch/x86/mm.c +4
-0@@ -6361,6 +6366,71 @@ static void __init __maybe_unused build_assertions(void)* using different PATs will not work. */ BUILD_BUG_ON (XEN_MSR_PAT !=0x050100070406ULL ); + +/* +* _PAGE_WB must be zero for several reasons, not least because Linux +* PV guests assume it.
xen/arch/x86/mm.c static void __init __maybe_unused build_assertions(void) · line 6382
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+*/ +BUILD_BUG_ON (_PAGE_WB); + +#define PAT_ENTRY (v) \ + (BUILD_BUG_ON_ZERO(((v) < 0) || ((v) > 7)) + \ + (0xFF & (XEN_MSR_PAT >> (8 * (v))))) + +/* Validate at compile-time that v is a valid value for a PAT entry */ +#define CHECK_PAT_ENTRY_VALUE (v) \ + BUILD_BUG_ON((v) < 0 || (v) > 7 || \
See my v5 comments.
Jan
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
In reply to Demi Marie ObenourQuoted 4 lines · Demi Marie Obenour
Setting cacheability flags that are not ones specified by Xen is a bug in the guest. By default, return
-EINVALif a guests attempts to do this. The invalid-cacheability= Xen command-line flag allows the administrator to allow such attempts or to produce
Unfinished sentence?
Quoted 19 lines
xen/arch/x86/mm.c +13
-0@@ -1324,6 +1324,37 @@ static int put_page_from_l4e(l4_pgentry_t l4e, mfn_t l4mfn, unsigned int flags)return put_pt_page (l4e_get_page (l4e),mfn_to_page (l4mfn), flags); } +enum { +INVALID_CACHEABILITY_ALLOW , +INVALID_CACHEABILITY_DENY , +INVALID_CACHEABILITY_TRAP , +}; + +#ifdef NDEBUG +#define INVALID_CACHEABILITY_DEFAULT INVALID_CACHEABILITY_DENY +#else +#define INVALID_CACHEABILITY_DEFAULT INVALID_CACHEABILITY_TRAP +#endif + +static __ro_after_init uint8_t invalid_cacheability =
xen/arch/x86/mm.c static int put_page_from_l4e(l4_pgentry_t l4e, mfn_t l4mfn, unsigned int flags) · line 1349
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+INVALID_CACHEABILITY_DEFAULT ; + +static int __init cf_check set_invalid_cacheability(const char *str) +{ +if (strcmp ("allow" , str) ==0 ) + invalid_cacheability =INVALID_CACHEABILITY_ALLOW ; +else if (strcmp ("deny" , str) ==0 ) + invalid_cacheability =INVALID_CACHEABILITY_DENY ; +else if (strcmp ("trap" , str) ==0 ) + invalid_cacheability =INVALID_CACHEABILITY_TRAP ;
Style: Missing blanks immediately inside if(). Also note that generally we prefer '!' over "== 0".
xen/arch/x86/mm.c static int put_page_from_l4e(l4_pgentry_t l4e, mfn_t l4mfn, unsigned int flags) · line 1356
Quoted 7 lines
xen/arch/x86/mm.c +7
-0+else +return -EINVAL ; + +return 0 ; +} + +custom_param ("invalid-cacheability" , set_invalid_cacheability);
Nit: Generally we avoid blank lines between the handler of a custom_param() and the actual param definition.
xen/arch/x86/mm.c static int promote_l1_table(struct page_info *page) · line 1381
Quoted 10 lines
xen/arch/x86/mm.c +5
-1@@ -1343,7 +1374,34 @@ static int promote_l1_table(struct page_info *page) }else { - switch ( ret = get_page_from_l1e(pl1e[i], d, d) ) +l1_pgentry_t l1e = pl1e[i]; + +if ( invalid_cacheability !=INVALID_CACHEABILITY_ALLOW ) + { + switch ( l1e.l1 & PAGE_CACHE_ATTRS )
You want to use l1e_get_flags() here.
Jan
Re: [PATCH v6 3/5] x86/mm: make code robust to future PAT changes
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
xen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6356
Quoted 8 lines · Demi Marie Obenour
xen/arch/x86/mm.c +2
-0@@ -6352,6 +6352,11 @@ unsigned long get_upper_mfn_bound(void)return min (max_mfn,1UL << (paddr_bits -PAGE_SHIFT )) -1 ; } + +/* Nit: Please avoid introducing double blank lines.
Quoted 7 lines
xen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6358
Quoted 2 lines
xen/arch/x86/mm.c +2
-0+* A bunch of static assertions to check that the XEN_MSR_PAT is valid +* and consistent with the _PAGE_* macros, and that _PAGE_WB is zero. This comment is too specific for a function of ...
xen/arch/x86/mm.c unsigned long get_upper_mfn_bound(void) · line 6360
Quoted 2 lines
xen/arch/x86/mm.c +1
-0+*/ static void __init __maybe_unused build_assertions (void )... this name, in this file.
IMO, you really don't need to comment build_assertions(). It's a pattern we use elsewhere, and the BUILD_BUG_ON()'s are individually commented.
I'd just drop this hunk entirely.
~Andrew
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
In reply to Demi Marie ObenourQuoted 9 lines · Demi Marie Obenour
docs/misc/xen-command-line.pandoc +1
-0@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86)
docs/misc/xen-command-line.pandoc detection of systems known to misbehave upon accesses to that port. · line 1430
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. +
This wants to be scoped under "pv", and not a top-level option. Current parsing is at the top of xen/arch/x86/pv/domain.c
How about pv={no-}oob-pat, and parse it into the opt_pv_oob_pat boolean ?
There really is no need to distinguish between deny and trap. IMO, injecting #GP unilaterally is fine (to a guest expecting the hypercall to succeed, -EINVAL vs #GP makes no difference, but #GP is far more useful to a human trying to debug issues here), but I could be talked into putting that behind a CONFIG_DEBUG if other feel strongly.
Quoted 21 lines
docs/misc/xen-command-line.pandoc +16
-1@@ -1343,7 +1374,34 @@ static int promote_l1_table(struct page_info *page) } else { - switch ( ret = get_page_from_l1e(pl1e[i], d, d) ) + l1_pgentry_t l1e = pl1e[i]; + + if ( invalid_cacheability != INVALID_CACHEABILITY_ALLOW ) + { + switch ( l1e.l1 & PAGE_CACHE_ATTRS ) + { + case _PAGE_WB: + case _PAGE_UC: + case _PAGE_UCM: + case _PAGE_WC: + case _PAGE_WT: + case _PAGE_WP: + break; + default: + /* + * If we get here, a PV guest tried to use one of the
docs/misc/xen-command-line.pandoc static int promote_l1_table(struct page_info *page) · line 1402
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+ * reserved values in Xen's PAT. This indicates a bug + * in the guest. If requested by the user, inject #GP + * to cause the guest to log a stack trace. + */ + if ( invalid_cacheability == INVALID_CACHEABILITY_TRAP ) + pv_inject_hw_exception(TRAP_gp_fault, 0); + ret = -EINVAL; + goto fail; + } + }
This will catch cases where the guest writes a full pagetable, then promotes it, but won't catch cases where the guest modifies an individual entry with mmu_update/etc.
The logic needs to be in get_page_from_l1e() to get applied uniformly to all PTE modifications.
Right now, the l1_disallow_mask() check near the start hides the "can you use a nonzero cacheattr" check. If I ever get around to cleaning up my post-XSA-402 series, I have a load of modifications to this.
But this could be something like this:
if ( opt_pv_oob_pat && (l1f & PAGE_CACHE_ATTRS) > _PAGE_WP ) {
// #GP
return -EINVAL;
}
fairly early on.
It occurs to me that this check is only applicable when we're using the Xen ABI APT, and seeing as we've talked about possibly making patch 5 a Kconfig option, that may want accounting here. (Perhaps simply making opt_pb_oob_pat be false in a !XEN_PAT build.)
~Andrew
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
Quoted 24 lines · Andrew Cooper
Quoted 9 lines · Demi Marie Obenour
docs/misc/xen-command-line.pandoc +1
-0@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86)docs/misc/xen-command-line.pandoc detection of systems known to misbehave upon accesses to that port. · line 1430
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. +This wants to be scoped under "pv", and not a top-level option. Current parsing is at the top of
xen/arch/x86/pv/domain.c
Quoted 1 line
How about
pv={no-}oob-pat, and parse it into theopt_pv_oob_patboolean ?
Works for me, though I will use ‘invalid’ instead of ‘oob’ as valid PAT entries might not be contiguous.
Quoted 5 lines
There really is no need to distinguish between deny and trap. IMO, injecting #GP unilaterally is fine (to a guest expecting the hypercall to succeed,
-EINVALvs #GP makes no difference, but #GP is far more useful to a human trying to debug issues here), but I could be talked into putting that behind aCONFIG_DEBUGif other feel strongly.
Marek, thoughts?
Quoted 36 lines
Quoted 21 lines
docs/misc/xen-command-line.pandoc +16
-1@@ -1343,7 +1374,34 @@ static int promote_l1_table(struct page_info *page) } else { - switch ( ret = get_page_from_l1e(pl1e[i], d, d) ) + l1_pgentry_t l1e = pl1e[i]; + + if ( invalid_cacheability != INVALID_CACHEABILITY_ALLOW ) + { + switch ( l1e.l1 & PAGE_CACHE_ATTRS ) + { + case _PAGE_WB: + case _PAGE_UC: + case _PAGE_UCM: + case _PAGE_WC: + case _PAGE_WT: + case _PAGE_WP: + break; + default: + /* + * If we get here, a PV guest tried to use one of thedocs/misc/xen-command-line.pandoc static int promote_l1_table(struct page_info *page) · line 1402
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+ * reserved values in Xen's PAT. This indicates a bug + * in the guest. If requested by the user, inject #GP + * to cause the guest to log a stack trace. + */ + if ( invalid_cacheability == INVALID_CACHEABILITY_TRAP ) + pv_inject_hw_exception(TRAP_gp_fault, 0); + ret = -EINVAL; + goto fail; + } + }This will catch cases where the guest writes a full pagetable, then promotes it, but won't catch cases where the guest modifies an individual entry with mmu_update/etc.
Quoted 2 lines
The logic needs to be in
get_page_from_l1e()to get applied uniformly to all PTE modifications.
I will move it there, and also update Qubes OS’s patchset.
Quoted 3 lines
Right now, the
l1_disallow_mask()check near the start hides the "can you use a nonzero cacheattr" check. If I ever get around to cleaning up my post-XSA-402 series, I have a load of modifications to this.
I came up with some major cleanups too.
Quoted 8 lines
But this could be something like this:
if (
opt_pv_oob_pat&& (l1f &PAGE_CACHE_ATTRS) >_PAGE_WP) {
// #GP
return-EINVAL;
}
Quoted 6 lines
fairly early on.
It occurs to me that this check is only applicable when we're using the Xen ABI APT, and seeing as we've talked about possibly making patch 5 a
Kconfigoption, that may want accounting here. (Perhaps simply makingopt_pb_oob_patbe false in a !XEN_PAT build.)
It’s actually applicable even with other PATs. While Marek and I were tracking down an Intel iGPU cache coherency problem, Marek used it to verify that PAT entries that we thought were not being used were in fact unused. This allowed proving that the behavior of the GPU was impacted by changes to PAT entries the hardware should not even be looking at, and therefore that the hardware itself must be buggy.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
Quoted 25 lines · Demi Marie Obenour
Quoted 24 lines · Andrew Cooper
Quoted 9 lines · Demi Marie Obenour
docs/misc/xen-command-line.pandoc +1
-0@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86)docs/misc/xen-command-line.pandoc detection of systems known to misbehave upon accesses to that port. · line 1430
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. +This wants to be scoped under "pv", and not a top-level option. Current parsing is at the top of
xen/arch/x86/pv/domain.c
Quoted 4 lines
Quoted 1 line
How about
pv={no-}oob-pat, and parse it into theopt_pv_oob_patboolean ?Works for me, though I will use ‘invalid’ instead of ‘oob’ as valid PAT entries might not be contiguous.
If we're talking about alternative PAT settings, I'm not sure if they can be called "invalid". With the default Xen's choice of PAT, top two entries are documented as reserved (in xen.h) and only that makes them forbidden to use. But with alternative settings, it's only behavior of Linux parsing that prefers using lower options. When breaking contract set in xen.h, "reserved" entries stop being reserved too.
So, _if_ that option would be applicable alternative PAT choice, it's only useful for debugging Linux specifically (assuming Linux won't change its approach to choosing entries - which I think it's allowed to do).
Quoted 7 lines
Quoted 5 lines
There really is no need to distinguish between deny and trap. IMO, injecting #GP unilaterally is fine (to a guest expecting the hypercall to succeed,
-EINVALvs #GP makes no difference, but #GP is far more useful to a human trying to debug issues here), but I could be talked into putting that behind aCONFIG_DEBUGif other feel strongly.Marek, thoughts?
With Xen's default PAT, #GP may be useful indeed, but it must come with a message why it was injected.
Quoted 62 lines
Quoted 36 lines
Quoted 21 lines
docs/misc/xen-command-line.pandoc +16
-1@@ -1343,7 +1374,34 @@ static int promote_l1_table(struct page_info *page) } else { - switch ( ret = get_page_from_l1e(pl1e[i], d, d) ) + l1_pgentry_t l1e = pl1e[i]; + + if ( invalid_cacheability != INVALID_CACHEABILITY_ALLOW ) + { + switch ( l1e.l1 & PAGE_CACHE_ATTRS ) + { + case _PAGE_WB: + case _PAGE_UC: + case _PAGE_UCM: + case _PAGE_WC: + case _PAGE_WT: + case _PAGE_WP: + break; + default: + /* + * If we get here, a PV guest tried to use one of thedocs/misc/xen-command-line.pandoc static int promote_l1_table(struct page_info *page) · line 1402
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+ * reserved values in Xen's PAT. This indicates a bug + * in the guest. If requested by the user, inject #GP + * to cause the guest to log a stack trace. + */ + if ( invalid_cacheability == INVALID_CACHEABILITY_TRAP ) + pv_inject_hw_exception(TRAP_gp_fault, 0); + ret = -EINVAL; + goto fail; + } + }This will catch cases where the guest writes a full pagetable, then promotes it, but won't catch cases where the guest modifies an individual entry with mmu_update/etc.
Quoted 2 lines
The logic needs to be in
get_page_from_l1e()to get applied uniformly to all PTE modifications.I will move it there, and also update Qubes OS’s patchset.
Quoted 3 lines
Right now, the
l1_disallow_mask()check near the start hides the "can you use a nonzero cacheattr" check. If I ever get around to cleaning up my post-XSA-402 series, I have a load of modifications to this.I came up with some major cleanups too.
Quoted 8 lines
But this could be something like this:
if (
opt_pv_oob_pat&& (l1f &PAGE_CACHE_ATTRS) >_PAGE_WP) {
// #GP
return-EINVAL;
}Quoted 6 lines
fairly early on.
It occurs to me that this check is only applicable when we're using the Xen ABI APT, and seeing as we've talked about possibly making patch 5 a
Kconfigoption, that may want accounting here. (Perhaps simply makingopt_pb_oob_patbe false in a !XEN_PAT build.)
Quoted 6 lines
It’s actually applicable even with other PATs. While Marek and I were tracking down an Intel iGPU cache coherency problem, Marek used it to verify that PAT entries that we thought were not being used were in fact unused. This allowed proving that the behavior of the GPU was impacted by changes to PAT entries the hardware should not even be looking at, and therefore that the hardware itself must be buggy.
In fact, I did that via WARN() on the Linux side, to _not_ have guest killed in this case, to potentially collect more info. As said above, with alternative PAT settings, the contract which entries are "valid" isn't there anymore, so punishing guest for using them isn't appropriate. It could still be useful feature for debugging Linux (and it feels, we'll need this feature for some time...). So, with !XEN_PAT it should be at least disabled by default, or maybe even hidden behind CONFIG_DEBUG.
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
Quoted 31 lines · Marek Marczykowski-Górecki
Quoted 25 lines · Demi Marie Obenour
Quoted 24 lines · Andrew Cooper
Quoted 9 lines · Demi Marie Obenour
docs/misc/xen-command-line.pandoc +1
-0@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86)docs/misc/xen-command-line.pandoc detection of systems known to misbehave upon accesses to that port. · line 1430
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. +This wants to be scoped under "pv", and not a top-level option. Current parsing is at the top of
xen/arch/x86/pv/domain.cQuoted 4 lines
Quoted 1 line
How about
pv={no-}oob-pat, and parse it into theopt_pv_oob_patboolean ?Works for me, though I will use ‘invalid’ instead of ‘oob’ as valid PAT entries might not be contiguous.
Quoted 6 lines
If we're talking about alternative PAT settings, I'm not sure if they can be called "invalid". With the default Xen's choice of PAT, top two entries are documented as reserved (in
xen.h) and only that makes them forbidden to use. But with alternative settings, it's only behavior of Linux parsing that prefers using lower options. When breaking contract set inxen.h, "reserved" entries stop being reserved too.
That makes sense.
Quoted 4 lines
So, _if_ that option would be applicable alternative PAT choice, it's only useful for debugging Linux specifically (assuming Linux won't change its approach to choosing entries - which I think it's allowed to do).
Point taken.
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
There really is no need to distinguish between deny and trap. IMO, injecting #GP unilaterally is fine (to a guest expecting the hypercall to succeed,
-EINVALvs #GP makes no difference, but #GP is far more useful to a human trying to debug issues here), but I could be talked into putting that behind aCONFIG_DEBUGif other feel strongly.Marek, thoughts?
With Xen's default PAT, #GP may be useful indeed, but it must come with a message why it was injected.
In xl dmesg?
Quoted 69 lines
Quoted 62 lines
Quoted 36 lines
Quoted 21 lines
docs/misc/xen-command-line.pandoc +16
-1@@ -1343,7 +1374,34 @@ static int promote_l1_table(struct page_info *page) } else { - switch ( ret = get_page_from_l1e(pl1e[i], d, d) ) + l1_pgentry_t l1e = pl1e[i]; + + if ( invalid_cacheability != INVALID_CACHEABILITY_ALLOW ) + { + switch ( l1e.l1 & PAGE_CACHE_ATTRS ) + { + case _PAGE_WB: + case _PAGE_UC: + case _PAGE_UCM: + case _PAGE_WC: + case _PAGE_WT: + case _PAGE_WP: + break; + default: + /* + * If we get here, a PV guest tried to use one of thedocs/misc/xen-command-line.pandoc static int promote_l1_table(struct page_info *page) · line 1402
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+ * reserved values in Xen's PAT. This indicates a bug + * in the guest. If requested by the user, inject #GP + * to cause the guest to log a stack trace. + */ + if ( invalid_cacheability == INVALID_CACHEABILITY_TRAP ) + pv_inject_hw_exception(TRAP_gp_fault, 0); + ret = -EINVAL; + goto fail; + } + }This will catch cases where the guest writes a full pagetable, then promotes it, but won't catch cases where the guest modifies an individual entry with mmu_update/etc.
Quoted 2 lines
The logic needs to be in
get_page_from_l1e()to get applied uniformly to all PTE modifications.I will move it there, and also update Qubes OS’s patchset.
Quoted 3 lines
Right now, the
l1_disallow_mask()check near the start hides the "can you use a nonzero cacheattr" check. If I ever get around to cleaning up my post-XSA-402 series, I have a load of modifications to this.I came up with some major cleanups too.
Quoted 8 lines
But this could be something like this:
if (
opt_pv_oob_pat&& (l1f &PAGE_CACHE_ATTRS) >_PAGE_WP) {
// #GP
return-EINVAL;
}Quoted 6 lines
fairly early on.
It occurs to me that this check is only applicable when we're using the Xen ABI APT, and seeing as we've talked about possibly making patch 5 a
Kconfigoption, that may want accounting here. (Perhaps simply makingopt_pb_oob_patbe false in a !XEN_PAT build.)Quoted 6 lines
It’s actually applicable even with other PATs. While Marek and I were tracking down an Intel iGPU cache coherency problem, Marek used it to verify that PAT entries that we thought were not being used were in fact unused. This allowed proving that the behavior of the GPU was impacted by changes to PAT entries the hardware should not even be looking at, and therefore that the hardware itself must be buggy.
Quoted 2 lines
In fact, I did that via
WARN()on the Linux side, to _not_ have guest killed in this case, to potentially collect more info.
Whoops! I must have misunderstood what you meant by "trap".
Quoted 7 lines
As said above,
with alternative PAT settings, the contract which entries are "valid" isn't there anymore, so punishing guest for using them isn't appropriate. It could still be useful feature for debugging Linux (and it feels, we'll need this feature for some time...). So, with !XEN_PAT it should be at least disabled by default, or maybe even hidden behindCONFIG_DEBUG.
Okay, makes sense.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Re: [PATCH v6 4/5] x86/mm: Reject invalid cacheability in PV guests by default
In reply to Andrew CooperQuoted 24 lines · Andrew Cooper
Quoted 9 lines · Demi Marie Obenour
docs/misc/xen-command-line.pandoc +1
-0@@ -1417,6 +1417,17 @@ detection of systems known to misbehave upon accesses to that port. ### idle_latency_factor (x86) > `= <integer>` +### invalid-cacheability (x86)docs/misc/xen-command-line.pandoc detection of systems known to misbehave upon accesses to that port. · line 1430
Quoted 10 lines
docs/misc/xen-command-line.pandoc +10
-0+> `= allow | deny | trap` + +> Default: `deny` in release builds, otherwise `trap` + +Specify what happens when a PV guest tries to use one of the reserved entries in +the PAT. `deny` causes the attempt to be rejected with -EINVAL, `allow` allows +the attempt, and `trap` causes a general protection fault to be raised. +Currently, the reserved entries are marked as uncacheable in Xen's PAT, but this +will change if new memory types are added, so guests must not rely on it. +This wants to be scoped under "pv", and not a top-level option. Current parsing is at the top of
xen/arch/x86/pv/domain.c
Quoted 7 lines
How about
pv={no-}oob-pat, and parse it into theopt_pv_oob_patboolean ?There really is no need to distinguish between deny and trap. IMO, injecting #GP unilaterally is fine (to a guest expecting the hypercall to succeed,
-EINVALvs #GP makes no difference, but #GP is far more useful to a human trying to debug issues here), but I could be talked into putting that behind aCONFIG_DEBUGif other feel strongly.
What is or is not useful to guests is guesswork. They might be "handling" failure with BUG(), in which case they'd still see a backtrace. So to me, as previously said, injecting #GP in the case here can only reasonably be a debugging aid (and hence be dependent upon DEBUG).
Jan