Skip to contentSkip to conversation

x86/mm: Suppresses vm_events caused by page-walks

PATCH In review Alexandru Isaila 4 participants 9 messages Sep 12, 2018 – Sep 19, 2018

v2 of 6 In review Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/mm: Suppresses vm_events caused by page-walks

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we need finer-grained control. We want to emulate the page-walk, but still get an EPT violation event if the current instruction would trigger one. This patch performs just the page-walk emulation.

---
Changes since V1:
- Changed guest_walk_tables() to set A bit on each level and check if there was any A set. If not the it will set the D bit according to the write flags and cr0.wp

---
 xen/arch/x86/mm/guest_walk.c     | 23 ++++++++++++++++++++++-
 xen/arch/x86/mm/hap/guest_walk.c | 32 +++++++++++++++++++++++++++++++-
 xen/arch/x86/mm/hap/hap.c        | 12 ++++++++----
 xen/arch/x86/mm/hap/private.h    | 10 ++++++++++
 xen/arch/x86/mm/mem_access.c     |  5 ++++-
 xen/arch/x86/mm/shadow/multi.c   |  6 +++---
 xen/include/asm-x86/guest_pt.h   |  3 ++-
 xen/include/asm-x86/paging.h     |  5 ++++-
 8 files changed, 84 insertions(+), 12 deletions(-)
xen/arch/x86/mm/guest_walk.c +22 -1
index f67aeda3d0..c99c48fa8a 100644
@@ -82,7 +82,8 @@ static bool set_ad_bits(guest_intpte_t *guest_p, guest_intpte_t *walk_p,
 bool
 guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
                   unsigned long va, walk_t *gw,
-                  uint32_t walk, mfn_t top_mfn, void *top_map)
+                  uint32_t walk, mfn_t top_mfn, void *top_map,
+                  bool set_ad)
 {
     struct domain *d = v->domain;
     p2m_type_t p2mt;
@@ -95,6 +96,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     uint32_t gflags, rc;
     unsigned int leaf_level;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
+    bool accessed = false;
 
 #define AR_ACCUM_AND (_PAGE_USER | _PAGE_RW)
 #define AR_ACCUM_OR  (_PAGE_NX_BIT)
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;
+
     /* Map the l3 table */
     l3p = map_domain_gfn(p2m,
                          guest_l4e_get_gfn(gw->l4e),
@@ -179,6 +185,10 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
+    if ( set_ad && set_ad_bits(&l3p[guest_l3_table_offset(va)].l3,
+                               &gw->l3e.l3, false) )
+        accessed = true;
+
     if ( gflags & _PAGE_PSE )
     {
         /*
@@ -278,6 +288,10 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
+    if ( set_ad && set_ad_bits(&l2p[guest_l2_table_offset(va)].l2,
+                               &gw->l2e.l2, false) )
+        accessed = true;
+
     if ( gflags & _PAGE_PSE )
     {
         /*
@@ -362,6 +376,13 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
      */
     ar = (ar_and & AR_ACCUM_AND) | (ar_or & AR_ACCUM_OR);
 
+    if ( set_ad )
+    {
+        set_ad_bits(&l1p[guest_l1_table_offset(va)].l1, &gw->l1e.l1,
+                    (ar & _PAGE_RW) && !accessed && !guest_wp_enabled(v));
+        goto out;
+    }
+
     /*
      * Sanity check.  If EFER.NX is disabled, _PAGE_NX_BIT is reserved and
      * should have caused a translation failure before we get here.
xen/arch/x86/mm/hap/guest_walk.c +31 -1
index 3b8ee2efce..4cbbf69095 100644
@@ -29,6 +29,10 @@ asm(".file \"" __OBJECT_FILE__ "\"");
 #define _hap_gva_to_gfn(levels) hap_gva_to_gfn_##levels##_levels
 #define hap_gva_to_gfn(levels) _hap_gva_to_gfn(levels)
 
+#define _hap_page_walk_set_ad_bits(levels)                                     \
+        hap_page_walk_set_ad_bits_##levels##_levels
+#define hap_page_walk_set_ad_bits(levels) _hap_page_walk_set_ad_bits(levels)
+
 #define _hap_p2m_ga_to_gfn(levels) hap_p2m_ga_to_gfn_##levels##_levels
 #define hap_p2m_ga_to_gfn(levels) _hap_p2m_ga_to_gfn(levels)
 
@@ -39,6 +43,32 @@ asm(".file \"" __OBJECT_FILE__ "\"");
 #include <asm/guest_pt.h>
 #include <asm/p2m.h>
 
+void hap_page_walk_set_ad_bits(GUEST_PAGING_LEVELS)(
+    struct vcpu *v, struct p2m_domain *p2m,
+    unsigned long va, uint32_t walk, unsigned long cr3)
+{
+    walk_t gw;
+    mfn_t top_mfn;
+    void *top_map;
+    gfn_t top_gfn;
+    struct page_info *top_page;
+    p2m_type_t p2mt;
+
+    top_gfn = _gfn(cr3 >> PAGE_SHIFT);
+    top_page = p2m_get_page_from_gfn(p2m, top_gfn, &p2mt, NULL,
+                                     P2M_ALLOC | P2M_UNSHARE);
+    top_mfn = page_to_mfn(top_page);
+
+    /* Map the top-level table and call the tree-walker */
+    ASSERT(mfn_valid(top_mfn));
+    top_map = map_domain_page(top_mfn);
+#if GUEST_PAGING_LEVELS == 3
+    top_map += (cr3 & ~(PAGE_MASK | 31));
+#endif
+
+    guest_walk_tables(v, p2m, va, &gw, walk, top_mfn, top_map, true);
+}
+
 unsigned long hap_gva_to_gfn(GUEST_PAGING_LEVELS)(
     struct vcpu *v, struct p2m_domain *p2m, unsigned long gva, uint32_t *pfec)
 {
@@ -91,7 +121,7 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(
 #if GUEST_PAGING_LEVELS == 3
     top_map += (cr3 & ~(PAGE_MASK | 31));
 #endif
-    walk_ok = guest_walk_tables(v, p2m, ga, &gw, *pfec, top_mfn, top_map);
+    walk_ok = guest_walk_tables(v, p2m, ga, &gw, *pfec, top_mfn, top_map, false);
     unmap_domain_page(top_map);
     put_page(top_page);
 
xen/arch/x86/mm/hap/hap.c +8 -4
index 3d651b94c3..ca046b78df 100644
@@ -768,7 +768,8 @@ static const struct paging_mode hap_paging_real_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 1
+    .guest_levels           = 1,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_2_levels
 };
 
 static const struct paging_mode hap_paging_protected_mode = {
@@ -779,7 +780,8 @@ static const struct paging_mode hap_paging_protected_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 2
+    .guest_levels           = 2,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_2_levels
 };
 
 static const struct paging_mode hap_paging_pae_mode = {
@@ -790,7 +792,8 @@ static const struct paging_mode hap_paging_pae_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 3
+    .guest_levels           = 3,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_3_levels
 };
 
 static const struct paging_mode hap_paging_long_mode = {
@@ -801,7 +804,8 @@ static const struct paging_mode hap_paging_long_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 4
+    .guest_levels           = 4,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_4_levels
 };
 
 /*
xen/arch/x86/mm/hap/private.h +10 -0
index 973fbe8be5..abb933c4f8 100644
@@ -47,4 +47,14 @@ unsigned long hap_p2m_ga_to_gfn_4_levels(struct vcpu *v,
     struct p2m_domain *p2m, unsigned long cr3,
     paddr_t ga, uint32_t *pfec, unsigned int *page_order);
 
+void hap_page_walk_set_ad_bits_2_levels(struct vcpu *v, struct p2m_domain *p2m,
+                                        unsigned long va, uint32_t walk,
+                                        unsigned long cr3);
+void hap_page_walk_set_ad_bits_3_levels(struct vcpu *v, struct p2m_domain *p2m,
+                                        unsigned long va, uint32_t walk,
+                                        unsigned long cr3);
+void hap_page_walk_set_ad_bits_4_levels(struct vcpu *v, struct p2m_domain *p2m,
+                                        unsigned long va,  uint32_t walk,
+                                        unsigned long cr3);
+
 #endif /* __HAP_PRIVATE_H__ */
xen/arch/x86/mm/mem_access.c +4 -1
index a8b3e99ec4..8b644946f0 100644
@@ -214,7 +214,10 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla,
          d->arch.monitor.inguest_pagefault_disabled &&
          npfec.kind != npfec_kind_with_gla ) /* don't send a mem_event */
     {
-        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op, X86_EVENT_NO_EC);
+        struct hvm_hw_cpu ctxt;
+
+        hvm_funcs.save_cpu_ctxt(v, &ctxt);
+        paging_get_hostmode(v)->page_walk_set_ad_bits(v, p2m, gla, 0, ctxt.cr3);
 
         return true;
     }
xen/arch/x86/mm/shadow/multi.c +3 -3
index 5cb216f0db..d3df9be57c 100644
@@ -178,12 +178,12 @@ sh_walk_guest_tables(struct vcpu *v, unsigned long va, walk_t *gw,
     return guest_walk_tables(v, p2m_get_hostp2m(v->domain), va, gw, pfec,
 #if GUEST_PAGING_LEVELS == 3 /* PAE */
                              INVALID_MFN,
-                             v->arch.paging.shadow.gl3e
+                             v->arch.paging.shadow.gl3e,
 #else /* 32 or 64 */
                              pagetable_get_mfn(v->arch.guest_table),
-                             v->arch.paging.shadow.guest_vtable
+                             v->arch.paging.shadow.guest_vtable,
 #endif
-                             );
+                            false);
 }
 
 /* This validation is called with lock held, and after write permission
xen/include/asm-x86/guest_pt.h +2 -1
index 8684b83fd6..3dfb7fa966 100644
@@ -425,7 +425,8 @@ static inline unsigned int guest_walk_to_page_order(const walk_t *gw)
 
 bool
 guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, unsigned long va,
-                  walk_t *gw, uint32_t pfec, mfn_t top_mfn, void *top_map);
+                  walk_t *gw, uint32_t pfec, mfn_t top_mfn, void *top_map,
+                  bool set_ad);
 
 /* Pretty-print the contents of a guest-walk */
 static inline void print_gw(const walk_t *gw)
xen/include/asm-x86/paging.h +4 -1
index b51e1709d3..076ca204f5 100644
@@ -127,7 +127,10 @@ struct paging_mode {
     void          (*write_p2m_entry       )(struct domain *d, unsigned long gfn,
                                             l1_pgentry_t *p, l1_pgentry_t new,
                                             unsigned int level);
-
+    void          (*page_walk_set_ad_bits )(struct vcpu *v,
+                                            struct p2m_domain *p2m,
+                                            unsigned long va, uint32_t walk,
+                                            unsigned long cr3);
     unsigned int guest_levels;
 
     /* paging support extension */
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we need finer-grained control. We want to emulate the page-walk, but still get an EPT violation event if the current instruction would trigger one. This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus on what you actually want to achieve?

As to the title: "Suppress ..." please.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 7 lines
xen/arch/x86/mm/guest_walk.c +3 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done later in the function) which needs thorough explanation.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 384

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +6 -0
@@ -362,6 +376,13 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
      */
     ar = (ar_and & AR_ACCUM_AND) | (ar_or & AR_ACCUM_OR);
 
+    if ( set_ad )
+    {
+        set_ad_bits(&l1p[guest_l1_table_offset(va)].l1, &gw->l1e.l1,
+                    (ar & _PAGE_RW) && !accessed && !guest_wp_enabled(v));
+        goto out;
+    }

Why does A bits being set (or not) in non-leaf (possibly, but even that's inconsistent in your handling) tables have any meaning for whether the D bit wants setting in the leaf page table? Similarly, why does it matter whether the accumulated permissions allow writing, irrespective of whether the operation was actually a write? Same for CR0.WP.

xen/arch/x86/mm/hap/hap.c static const struct paging_mode hap_paging_real_mode = { · line 773

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +2 -1
@@ -768,7 +768,8 @@ static const struct paging_mode hap_paging_real_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 1
+    .guest_levels           = 1,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_2_levels
 };

Here and elsewhere, please add new hooks next to other hooks, not at the end.

xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 219

Quoted 10 lines
xen/arch/x86/mm/mem_access.c +3 -1
@@ -214,7 +214,10 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla,
          d->arch.monitor.inguest_pagefault_disabled &&
          npfec.kind != npfec_kind_with_gla ) /* don't send a mem_event */
     {
-        hvm_emulate_one_vm_event(EMUL_KIND_NORMAL, TRAP_invalid_op, X86_EVENT_NO_EC);
+        struct hvm_hw_cpu ctxt;
+
+        hvm_funcs.save_cpu_ctxt(v, &ctxt);

Pretty expensive an operation when all you're after is v->arch.hvm.guest_cr[3].

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 7 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we
set any. If we set A bits, consider ourselves complete and exit back to
the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

If I misunderstood the comment please clarify.

Thanks,
Alex

Quoted 56 lines
Quoted 4 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -362,6 +376,13 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
      */

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct · line 387

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +6 -0
     ar = (ar_and & AR_ACCUM_AND) | (ar_or & AR_ACCUM_OR);
 
+    if ( set_ad )
+    {
+        set_ad_bits(&l1p[guest_l1_table_offset(va)].l1, &gw-
 >l1e.l1,
+                    (ar & _PAGE_RW) && !accessed &&
 !guest_wp_enabled(v));
+        goto out;
+    }

Why does A bits being set (or not) in non-leaf (possibly, but even that's
inconsistent in your handling) tables have any meaning for whether the
D bit wants setting in the leaf page table? Similarly, why does it matter
whether the accumulated permissions allow writing, irrespective of whether the operation was actually a write? Same for CR0.WP.

xen/arch/x86/mm/hap/hap.c static const struct paging_mode · line 774

Quoted 11 lines
xen/arch/x86/mm/hap/hap.c +2 -1
@@ -768,7 +768,8 @@ static const struct paging_mode
 hap_paging_real_mode = {
     .update_cr3             = hap_update_cr3,
     .update_paging_modes    = hap_update_paging_modes,
     .write_p2m_entry        = hap_write_p2m_entry,
-    .guest_levels           = 1
+    .guest_levels           = 1,
+    .page_walk_set_ad_bits  = hap_page_walk_set_ad_bits_2_levels
 };

Here and elsewhere, please add new hooks next to other hooks, not at the end.

xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 219

Quoted 13 lines
xen/arch/x86/mm/mem_access.c +0 -0
@@ -214,7 +214,10 @@ bool p2m_mem_access_check(paddr_t gpa,
 unsigned long gla,
          d->arch.monitor.inguest_pagefault_disabled &&
          npfec.kind != npfec_kind_with_gla ) /* don't send a

mem_event */

     {

- hvm_emulate_one_vm_event(EMUL_KIND_NORMAL,
TRAP_invalid_op, X86_EVENT_NO_EC);

xen/arch/x86/mm/mem_access.c +3 -0
+        struct hvm_hw_cpu ctxt;
+
+        hvm_funcs.save_cpu_ctxt(v, &ctxt);

Pretty expensive an operation when all you're after is v->arch.hvm.guest_cr[3].

Quoted 3 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 7 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

Quoted 6 lines

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 36 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 6 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

Quoted 8 lines
Quoted 1 line

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

The problem we have is that, while we know the EPT Violation was for a write of an A or D bit to a write-protected guest pagetable, we don't know if it was the A or the D bit which was attempting to be set.

Furthermore (without emulating the instruction, which is what we are trying to avoid), we can't reconstruct the access.

Access bits are only written if they were missing before, but may be set speculatively.  Dirty bits are only set when a write is retired.  From a practical point of view, the pipeline sets A and D bits as separate actions.

Following this logic (and assuming for now a single vcpu), if we get a GPT EPT Violation, and there are missing access bits on the walk, then the fault is definitely from setting an access bit.  Set all access bits and call it done.  If we get a GPT EPT Violation and all access bits were set, then it was definitely from setting the Dirty bit.

For multi-vcpu scenarios, things get racy.  Setting all the Access bits is safe because its a speculative action, but a speculatively load on one vcpu can race with a write (to a read-only mapping) on the other vcpu, and would trick this algorithm into setting the dirty bit when the write would have faulted (and not set the dirty bit).

Do we have numbers on how many the GPT EPT Violations are for (only) access sets, and how many are for dirty tsets?  Would the first half of the algorithm (which is definitely not racy) still be a net perf win?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 69 lines · Andrew Cooper
Quoted 36 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 6 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

Quoted 8 lines
Quoted 1 line

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

The problem we have is that, while we know the EPT Violation was for a write of an A or D bit to a write-protected guest pagetable, we don't know if it was the A or the D bit which was attempting to be set.

Furthermore (without emulating the instruction, which is what we are trying to avoid), we can't reconstruct the access.

Access bits are only written if they were missing before, but may be set speculatively.  Dirty bits are only set when a write is retired.  From a practical point of view, the pipeline sets A and D bits as separate actions.

Following this logic (and assuming for now a single vcpu), if we get a GPT EPT Violation, and there are missing access bits on the walk, then the fault is definitely from setting an access bit.  Set all access bits and call it done.  If we get a GPT EPT Violation and all access bits were set, then it was definitely from setting the Dirty bit.

For multi-vcpu scenarios, things get racy.  Setting all the Access bits is safe because its a speculative action, but a speculatively load on one vcpu can race with a write (to a read-only mapping) on the other vcpu, and would trick this algorithm into setting the dirty bit when the write would have faulted (and not set the dirty bit).

Quoted 3 lines

Do we have numbers on how many the GPT EPT Violations are for (only) access sets, and how many are for dirty tsets?  Would the first half of the algorithm (which is definitely not racy) still be a net perf win?

Last time I've counted with a simple test there were 25 Ds to 19062 As, so yes, most of these are setting access bits, and yes, it looks like it's still worth doing even when setting the A bits alone - though of course we'd prefer to avoid vm_events for both.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
In reply to Andrew Cooper
Quoted 54 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 20:20, <[email protected]> wrote:
Quoted 36 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 6 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

Quoted 8 lines
Quoted 1 line

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

The problem we have is that, while we know the EPT Violation was for a write of an A or D bit to a write-protected guest pagetable, we don't know if it was the A or the D bit which was attempting to be set.

Furthermore (without emulating the instruction, which is what we are trying to avoid), we can't reconstruct the access.

Quoted 7 lines

Access bits are only written if they were missing before, but may be set speculatively. Dirty bits are only set when a write is retired. From a practical point of view, the pipeline sets A and D bits as separate actions.

Following this logic (and assuming for now a single vcpu), if we get a GPT EPT Violation, and there are missing access bits on the walk, then the fault is definitely from setting an access bit.

Definitely? Is there anything guaranteeing architecturally that an access bit related EPT violation would be delivered earlier than any other one on that same or a lower page table level? It doesn't matter much for the implementation (because of it being permissible to set the A bits speculatively, as you also say further down, and any other violation then re-occurring after exiting back to the guest once the A bits are all set), but since we're discussing here what exactly the patch description should contain, I think I'd prefer this to be fully correct there.

Or wait - I think I can agree with "definitely", provided you further restrict the context: "..., if we get a GPT EPT Write Violation ...". But from what I can tell the patch'es change to p2m_mem_access_check() doesn't apply (or pass on) any of these qualifications at all.

Quoted 10 lines

Set all access bits
and call it done. If we get a GPT EPT Violation and all access bits were set, then it was definitely from setting the Dirty bit.

For multi-vcpu scenarios, things get racy. Setting all the Access bits is safe because its a speculative action, but a speculatively load on one vcpu can race with a write (to a read-only mapping) on the other vcpu, and would trick this algorithm into setting the dirty bit when the write would have faulted (and not set the dirty bit).

Quoted 3 lines

Do we have numbers on how many the GPT EPT Violations are for (only) access sets, and how many are for dirty tsets? Would the first half of the algorithm (which is definitely not racy) still be a net perf win?

Does Windows make use of A bits at all? I'd expect most OSes to simply set them right away, and actively use of the D bits.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 53 lines · Jan Beulich
Quoted 53 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 20:20, <[email protected]> wrote:
Quoted 36 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 6 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 156

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

Quoted 8 lines
Quoted 1 line

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

The problem we have is that, while we know the EPT Violation was for a write of an A or D bit to a write-protected guest pagetable, we don't know if it was the A or the D bit which was attempting to be set.

Furthermore (without emulating the instruction, which is what we are trying to avoid), we can't reconstruct the access.

Quoted 8 lines
Quoted 7 lines

Access bits are only written if they were missing before, but may be set speculatively. Dirty bits are only set when a write is retired. From a practical point of view, the pipeline sets A and D bits as separate actions.

Following this logic (and assuming for now a single vcpu), if we get a GPT EPT Violation, and there are missing access bits on the walk, then the fault is definitely from setting an access bit.

Definitely?

Yes

Quoted 3 lines

Is there anything guaranteeing architecturally that an access
bit related EPT violation would be delivered earlier than any other one on that same or a lower page table level?

No, but why does that matter?

Architecturally defined or not, we know that the action the processor was trying to perform was to set an A/D bit, because we got a vmexit telling us so.

Quoted 11 lines

It doesn't matter much for
the implementation (because of it being permissible to set the A bits speculatively, as you also say further down, and any other violation then re-occurring after exiting back to the guest once the A bits are all set), but since we're discussing here what exactly the patch description should contain, I think I'd prefer this to be fully correct there.

Or wait - I think I can agree with "definitely", provided you further restrict the context: "..., if we get a GPT EPT Write Violation ...". But from what I can tell the patch'es change to p2m_mem_access_check() doesn't apply (or pass on) any of these qualifications at all.

I've not looked at the patch in detail yet.  I'm tempted to suggest rearranging guest_walk_tables() to just set the access bits on the decent, rather than at the end.  This matches how some hardware behaves when pulling entries into the paging structure cache.

Quoted 11 lines
Quoted 11 lines

Set all access bits
and call it done. If we get a GPT EPT Violation and all access bits were set, then it was definitely from setting the Dirty bit.

For multi-vcpu scenarios, things get racy. Setting all the Access bits is safe because its a speculative action, but a speculatively load on one vcpu can race with a write (to a read-only mapping) on the other vcpu, and would trick this algorithm into setting the dirty bit when the write would have faulted (and not set the dirty bit).

Quoted 5 lines
Quoted 3 lines

Do we have numbers on how many the GPT EPT Violations are for (only) access sets, and how many are for dirty tsets? Would the first half of the algorithm (which is definitely not racy) still be a net perf win?

Does Windows make use of A bits at all? I'd expect most OSes to simply set them right away, and actively use of the D bits.

What gives you the expectation that OSes wouldn't use A bits?

For paging out, the best options are non-accessed non-dirty page because their contents can be discarded immediately and reread from disk at a later point.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 70 lines
Quoted 1 line
Quoted 1 line · On 19.09.18 at 15:41, <[email protected]> wrote:
Quoted 53 lines · Jan Beulich
Quoted 53 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 20:20, <[email protected]> wrote:
Quoted 36 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 18.09.18 at 11:47, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.18 at 11:47, <[email protected]> wrote:

The original version of the patch emulated the current instruction (which, as a side-effect, emulated the page-walk as well), however we
need finer-grained control. We want to emulate the page-walk, but still
get an EPT violation event if the current instruction would trigger one.
This patch performs just the page-walk emulation.

Rather than making this basically a revision log, could you please focus
on what you actually want to achieve?

As to the title: "Suppress ..." please.

Quoted 5 lines
(quoted diff) +0 -0
@@ -149,6 +151,10 @@ guest_walk_tables(struct vcpu *v, struct
 p2m_domain *p2m,
     ar_and &= gflags;
     ar_or  |= gflags;
 
Quoted 6 lines

Review guest_walk_tables(struct vcpu *v, struct · line 157

Quoted 3 lines
(quoted diff) +3 -0
+    if ( set_ad && set_ad_bits(&l4p[guest_l4_table_offset(va)].l4,
+                               &gw->l4e.l4, false) )
+        accessed = true;

It is in particular this seemingly odd (and redundant with what's done
later in the function) which needs thorough explanation.

On this patch I've followed Andrew Cooper's suggestion on how to set A/D Bits:

"While walking down the levels, set any missing A bits and remember if we set any. If we set A bits, consider ourselves complete and exit back to the guest. If no A bits were set, and the access was a write (which we know from the EPT violation information), then set the leaf D bit."

Quoted 8 lines
Quoted 1 line

If I misunderstood the comment please clarify.

It doesn't look to me as if you misunderstood anything, but only Andrew can say for sure. However, none of this was in the description of your patch (neither as part of the description, nor as code comment), and I think you'd even have to greatly extend on this in order to explain to everyone why the resulting behavior is still architecturally correct. In no case should you assume anyone reading your patch (now or in the future) has participated in the earlier discussion.

The problem we have is that, while we know the EPT Violation was for a write of an A or D bit to a write-protected guest pagetable, we don't know if it was the A or the D bit which was attempting to be set.

Furthermore (without emulating the instruction, which is what we are trying to avoid), we can't reconstruct the access.

Quoted 8 lines
Quoted 7 lines

Access bits are only written if they were missing before, but may be set speculatively. Dirty bits are only set when a write is retired. From a practical point of view, the pipeline sets A and D bits as separate actions.

Following this logic (and assuming for now a single vcpu), if we get a GPT EPT Violation, and there are missing access bits on the walk, then the fault is definitely from setting an access bit.

Definitely?

Yes

Quoted 3 lines

Is there anything guaranteeing architecturally that an access
bit related EPT violation would be delivered earlier than any other one on that same or a lower page table level?

Quoted 5 lines

No, but why does that matter?

Architecturally defined or not, we know that the action the processor was trying to perform was to set an A/D bit, because we got a vmexit telling us so.

Well, as per what I had written further down in my earlier reply (starting with "Or wait ..."), nothing in what you said about the EPT violation made explicit that this was a violation from a write attempt. Without that, it very much matters (afaict), as there are other reasons for getting EPT violations from page table accesses.

Quoted 12 lines
Quoted 11 lines

It doesn't matter much for
the implementation (because of it being permissible to set the A bits speculatively, as you also say further down, and any other violation then re-occurring after exiting back to the guest once the A bits are all set), but since we're discussing here what exactly the patch description should contain, I think I'd prefer this to be fully correct there.

Or wait - I think I can agree with "definitely", provided you further restrict the context: "..., if we get a GPT EPT Write Violation ...". But from what I can tell the patch'es change to p2m_mem_access_check() doesn't apply (or pass on) any of these qualifications at all.

Quoted 4 lines

I've not looked at the patch in detail yet. I'm tempted to suggest rearranging guest_walk_tables() to just set the access bits on the decent, rather than at the end. This matches how some hardware behaves when pulling entries into the paging structure cache.

I'm not opposed to that; I believe this property has changed over the years, as I certainly do recall that early (386ish, 486ish) descriptions of page walks suggested that A bits indeed got set after the full walk only, and I further believe the original implementation simply took this behavior as reference.

What I am not very happy about is the addition of a (poorly described) new parameter to the function, making it sort of bail in the middle.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Set all access bits
and call it done. If we get a GPT EPT Violation and all access bits were set, then it was definitely from setting the Dirty bit.

For multi-vcpu scenarios, things get racy. Setting all the Access bits is safe because its a speculative action, but a speculatively load on one vcpu can race with a write (to a read-only mapping) on the other vcpu, and would trick this algorithm into setting the dirty bit when the write would have faulted (and not set the dirty bit).

Do we have numbers on how many the GPT EPT Violations are for (only) access sets, and how many are for dirty tsets? Would the first half of the algorithm (which is definitely not racy) still be a net perf win?

Quoted 8 lines
Quoted 2 lines

Does Windows make use of A bits at all? I'd expect most OSes to simply set them right away, and actively use of the D bits.

What gives you the expectation that OSes wouldn't use A bits?

For paging out, the best options are non-accessed non-dirty page because their contents can be discarded immediately and reread from disk at a later point.

Discard-immediately is solely bound to the D bit. The A bit only tells an OS whether a page was accessed after the bit was cleared the last time. I will surely agree this is a possible strategy, and I was merely wondering whether Windows (other than iirc Linux) actually makes use of this. The reason I wouldn't really expect their use is because repeatedly (and perhaps frequently) clearing A bits from all PTEs is sort of work intensive (as is locating a PTE with its A bit clear). Yet if you don't clear them frequently, the information a set A bit carries does not look to be overly useful.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel