Skip to contentSkip to conversation

x86: add p2m_mmio_write_dm

PATCH In review Yu Zhang 3 participants 19 messages Nov 28, 2014 – Dec 4, 2014

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH v4 1/1 x86: add p2m_mmio_write_dm

XenGT (Intel Graphics Virtualization technology, please refer to
https://01.org/xen/blogs/srclarkx/2013/graphics-virtualization-
xengt) driver runs inside Dom0 as a virtual graphics device model, and needs to trap and emulate the guest's write operations to some specific memory pages, like memory pages used by guest graphics driver as PPGTT(per-process graphics translation table). We agreed to add a new p2m type "p2m_mmio_write_dm" to trap the write operation on these graphic page tables.

Handling of this new p2m type are similar with existing p2m_ram_ro in most condition checks, with only difference on final policy of emulation vs. drop. For p2m_mmio_write_dm type pages, writes will go to the device model via ioreq-server.

Previously, the conclusion in our v3 patch review is to provide a more generalized HVMOP_map_io_range_to_ioreq_server hypercall, by seperating rangesets inside a ioreq server to read-protected/write- protected/both-prtected. Yet, after offline discussion with Paul, we believe a more simplified solution may suffice. We can keep the existing HVMOP_map_io_range_to_ioreq_server hypercall, and let the user decide whether or not a p2m type change is necessary, because in most cases the emulator will already use the p2m_mmio_dm type.

Changes from v3:
- Use the existing HVMOP_map_io_range_to_ioreq_server hypercall to add write protected range.
- Modify the HVMOP_set_mem_type hypercall to support the new p2m type for this range

Changes from v2:
- Remove excute attribute of the new p2m type p2m_mmio_write_dm
- Use existing rangeset for keeping the write protection page range instead of introducing hash table.
- Some code style fix.

Changes from v1:
- Changes the new p2m type name from p2m_ram_wp to p2m_mmio_write_dm. This means that we treat the pages as a special mmio range instead of ram.
- Move macros to c file since only this file is using them.
- Address various comments from Jan.

---
 xen/arch/x86/hvm/hvm.c          | 13 ++++++++++---
 xen/arch/x86/mm/p2m-ept.c       |  1 +
 xen/arch/x86/mm/p2m-pt.c        |  1 +
 xen/include/asm-x86/p2m.h       |  1 +
 xen/include/public/hvm/hvm_op.h |  1 +
 5 files changed, 14 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hvm.c +10 -3
index 8f49b44..5f806e8 100644
@@ -2838,7 +2838,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
      * to the mmio handler.
      */
     if ( (p2mt == p2m_mmio_dm) || 
-         (npfec.write_access && (p2mt == p2m_ram_ro)) )
+         (npfec.write_access &&
+		((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm))) )
     {
         put_gfn(p2m->domain, gfn);
 
@@ -5922,6 +5923,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                 a.mem_type =  HVMMEM_ram_rw;
             else if ( p2m_is_grant(t) )
                 a.mem_type =  HVMMEM_ram_rw;
+            else if ( t == p2m_mmio_write_dm )
+                a.mem_type = HVMMEM_mmio_write_dm;
             else
                 a.mem_type =  HVMMEM_mmio_dm;
             rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
@@ -5941,7 +5944,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         static const p2m_type_t memtype[] = {
             [HVMMEM_ram_rw]  = p2m_ram_rw,
             [HVMMEM_ram_ro]  = p2m_ram_ro,
-            [HVMMEM_mmio_dm] = p2m_mmio_dm
+            [HVMMEM_mmio_dm] = p2m_mmio_dm,
+            [HVMMEM_mmio_write_dm] = p2m_mmio_write_dm
         };
 
         if ( copy_from_guest(&a, arg, 1) )
@@ -5987,14 +5991,17 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                 rc = -EAGAIN;
                 goto param_fail4;
             }
+
             if ( !p2m_is_ram(t) &&
-                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) )
+                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) &&
+                 t != p2m_mmio_write_dm )
             {
                 put_gfn(d, pfn);
                 goto param_fail4;
             }
 
             rc = p2m_change_type_one(d, pfn, t, memtype[a.hvmmem_type]);
+
             put_gfn(d, pfn);
             if ( rc )
                 goto param_fail4;
xen/arch/x86/mm/p2m-ept.c +1 -0
index 15c6e83..e21a92d 100644
@@ -136,6 +136,7 @@ static void ept_p2m_type_to_flags(ept_entry_t *entry, p2m_type_t type, p2m_acces
             entry->x = 0;
             break;
         case p2m_grant_map_ro:
+        case p2m_mmio_write_dm:
             entry->r = 1;
             entry->w = entry->x = 0;
             break;
xen/arch/x86/mm/p2m-pt.c +1 -0
index e48b63a..26fb18d 100644
@@ -94,6 +94,7 @@ static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn)
     default:
         return flags | _PAGE_NX_BIT;
     case p2m_grant_map_ro:
+    case p2m_mmio_write_dm:
         return flags | P2M_BASE_FLAGS | _PAGE_NX_BIT;
     case p2m_ram_ro:
     case p2m_ram_logdirty:
xen/include/asm-x86/p2m.h +1 -0
index 5f7fe71..a7a45af 100644
@@ -72,6 +72,7 @@ typedef enum {
     p2m_ram_shared = 12,          /* Shared or sharable memory */
     p2m_ram_broken = 13,          /* Broken page, access cause domain crash */
     p2m_map_foreign  = 14,        /* ram pages from foreign domain */
+    p2m_mmio_write_dm = 15,       /* Read-only; writes go to the device model */
 } p2m_type_t;
 
 /* Modifiers to the query */
xen/include/public/hvm/hvm_op.h +1 -0
index eeb0a60..a4e5345 100644
@@ -81,6 +81,7 @@ typedef enum {
     HVMMEM_ram_rw,             /* Normal read/write guest RAM */
     HVMMEM_ram_ro,             /* Read-only; writes are discarded */
     HVMMEM_mmio_dm,            /* Reads and write go to the device model */
+    HVMMEM_mmio_write_dm       /* Read-only; writes go to the device model */
 } hvmmem_type_t;
 
 /* Following tools-only interfaces may change in future. */
Signature · 1 line
1.9.1
#1 lore raw

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2842

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.11.14 at 08:59, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2838,7 +2838,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
      * to the mmio handler.
      */
     if ( (p2mt == p2m_mmio_dm) || 
-         (npfec.write_access && (p2mt == p2m_ram_ro)) )
+         (npfec.write_access &&
+		((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm))) )

Why are you corrupting indentation here?

Furthermore the code you modify here suggests that p2m_ram_ro already has the needed semantics - writes get passed to the DM. None of the other changes you make, and none of the other uses of p2m_ram_ro appear to be in conflict with your intentions, so you'd really need to explain better why you need the new type.

Quoted 20 lines
xen/arch/x86/hvm/hvm.c +4 -1
@@ -5922,6 +5923,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                 a.mem_type =  HVMMEM_ram_rw;
             else if ( p2m_is_grant(t) )
                 a.mem_type =  HVMMEM_ram_rw;
+            else if ( t == p2m_mmio_write_dm )
+                a.mem_type = HVMMEM_mmio_write_dm;
             else
                 a.mem_type =  HVMMEM_mmio_dm;
             rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
@@ -5941,7 +5944,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         static const p2m_type_t memtype[] = {
             [HVMMEM_ram_rw]  = p2m_ram_rw,
             [HVMMEM_ram_ro]  = p2m_ram_ro,
-            [HVMMEM_mmio_dm] = p2m_mmio_dm
+            [HVMMEM_mmio_dm] = p2m_mmio_dm,
+            [HVMMEM_mmio_write_dm] = p2m_mmio_write_dm
         };
 
         if ( copy_from_guest(&a, arg, 1) )

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5994

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -5987,14 +5991,17 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                 rc = -EAGAIN;
                 goto param_fail4;
             }
+

Stray addition of a blank line?

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5997

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -1
             if ( !p2m_is_ram(t) &&
-                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) )
+                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) &&
+                 t != p2m_mmio_write_dm )

Do you really want to permit e.g. transitions between mmio_dm and mmio_write_dm? We should be as restrictive as possible here to not open up paths to security problems.

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 6007

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +1 -0
             {
                 put_gfn(d, pfn);
                 goto param_fail4;
             }
 
             rc = p2m_change_type_one(d, pfn, t, memtype[a.hvmmem_type]);
+
             put_gfn(d, pfn);
             if ( rc )
                 goto param_fail4;

Another stray newline addition.

xen/include/asm-x86/p2m.h typedef enum { · line 78

Quoted 11 lines
xen/include/asm-x86/p2m.h +1 -0
@@ -72,6 +72,7 @@ typedef enum {
     p2m_ram_shared = 12,          /* Shared or sharable memory */
     p2m_ram_broken = 13,          /* Broken page, access cause domain crash */
     p2m_map_foreign  = 14,        /* ram pages from foreign domain */
+    p2m_mmio_write_dm = 15,       /* Read-only; writes go to the device model */
 } p2m_type_t;
 
 /* Modifiers to the query */

If the new type is really needed, shouldn't this get added to P2M_RO_TYPES?

Jan

author
#2 lore raw
Quoted 12 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2842

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.11.14 at 08:59, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2838,7 +2838,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
       * to the mmio handler.
       */
      if ( (p2mt == p2m_mmio_dm) ||
-         (npfec.write_access && (p2mt == p2m_ram_ro)) )
+         (npfec.write_access &&
+		((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm))) )

Why are you corrupting indentation here?

Thanks for your comments, Jan.
The indentation here is to make sure the
((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm)) are grouped together. But I am not sure if this is correct according to xen coding style. I may have misunderstood your previous comments on Sep 3, which said "the indentation would need adjustment" in reply to "[Xen-devel] [PATCH v3 1/2] x86: add p2m_mmio_write_dm"

Quoted 7 lines

Furthermore the code you modify here suggests that p2m_ram_ro already has the needed semantics - writes get passed to the DM. None of the other changes you make, and none of the other uses of p2m_ram_ro appear to be in conflict with your intentions, so you'd really need to explain better why you need the new type.

Thanks Jan.
To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Maybe I need to update my commit message to explain this more clearly. :)

Quoted 58 lines
Quoted 20 lines
xen/arch/x86/hvm/hvm.c +4 -1
@@ -5922,6 +5923,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                  a.mem_type =  HVMMEM_ram_rw;
              else if ( p2m_is_grant(t) )
                  a.mem_type =  HVMMEM_ram_rw;
+            else if ( t == p2m_mmio_write_dm )
+                a.mem_type = HVMMEM_mmio_write_dm;
              else
                  a.mem_type =  HVMMEM_mmio_dm;
              rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
@@ -5941,7 +5944,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
          static const p2m_type_t memtype[] = {
              [HVMMEM_ram_rw]  = p2m_ram_rw,
              [HVMMEM_ram_ro]  = p2m_ram_ro,
-            [HVMMEM_mmio_dm] = p2m_mmio_dm
+            [HVMMEM_mmio_dm] = p2m_mmio_dm,
+            [HVMMEM_mmio_write_dm] = p2m_mmio_write_dm
          };
 
          if ( copy_from_guest(&a, arg, 1) )

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5994

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -5987,14 +5991,17 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
                  rc = -EAGAIN;
                  goto param_fail4;
              }
+

Stray addition of a blank line?

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5997

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -1
              if ( !p2m_is_ram(t) &&
-                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) )
+                 (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) &&
+                 t != p2m_mmio_write_dm )

Do you really want to permit e.g. transitions between mmio_dm and mmio_write_dm? We should be as restrictive as possible here to not open up paths to security problems.

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 6007

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +1 -0
              {
                  put_gfn(d, pfn);
                  goto param_fail4;
              }
 
              rc = p2m_change_type_one(d, pfn, t, memtype[a.hvmmem_type]);
+
              put_gfn(d, pfn);
              if ( rc )
                  goto param_fail4;

Another stray newline addition.

Quoted 9 lines
xen/include/asm-x86/p2m.h +1 -0
@@ -72,6 +72,7 @@ typedef enum {
      p2m_ram_shared = 12,          /* Shared or sharable memory */
      p2m_ram_broken = 13,          /* Broken page, access cause domain crash */
      p2m_map_foreign  = 14,        /* ram pages from foreign domain */
+    p2m_mmio_write_dm = 15,       /* Read-only; writes go to the device model */
  } p2m_type_t;
 
Quoted 6 lines

xen/include/asm-x86/p2m.h typedef enum { · line 78

Quoted 2 lines
xen/include/asm-x86/p2m.h +0 -0
  /* Modifiers to the query */

If the new type is really needed, shouldn't this get added to P2M_RO_TYPES?

Well, previsouly, I wished to differenciate the HVMMEM_ram_ro and the newly added HVMMEM_mmio_write_dm in the HVMOP_get_mem_type/HVMOP_set_mem_type hypercall. I'd rather think of this new type as write protected than read only. But I'll take a look, to see if this can be added to P2M_RO_TYPES. Thanks.

Yu

Quoted 9 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2842

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.11.14 at 08:59, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2838,7 +2838,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
       * to the mmio handler.
       */
      if ( (p2mt == p2m_mmio_dm) ||
-         (npfec.write_access && (p2mt == p2m_ram_ro)) )
+         (npfec.write_access &&
+		((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm))) )

Why are you corrupting indentation here?

Quoted 7 lines

Thanks for your comments, Jan.
The indentation here is to make sure the
((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm)) are grouped together. But I am not sure if this is correct according to xen coding style. I may have misunderstood your previous comments on Sep 3, which said "the indentation would need adjustment" in reply to "[Xen-devel] [PATCH v3 1/2] x86: add p2m_mmio_write_dm"

Getting the alignment right is needed, but no via using hard tabs.

Quoted 6 lines
Quoted 6 lines

Furthermore the code you modify here suggests that p2m_ram_ro already has the needed semantics - writes get passed to the DM. None of the other changes you make, and none of the other uses of p2m_ram_ro appear to be in conflict with your intentions, so you'd really need to explain better why you need the new type.

Quoted 9 lines

Thanks Jan.
To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

Quoted 1 line

Maybe I need to update my commit message to explain this more clearly. :)

At the very least, yes.

Jan

#4 lore raw
Quoted 40 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2842

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 28.11.14 at 08:59, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2838,7 +2838,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
       * to the mmio handler.
       */
      if ( (p2mt == p2m_mmio_dm) ||
-         (npfec.write_access && (p2mt == p2m_ram_ro)) )
+         (npfec.write_access &&
+		((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm))) )

Why are you corrupting indentation here?

Quoted 7 lines

Thanks for your comments, Jan.
The indentation here is to make sure the
((p2mt == p2m_ram_ro) || (p2mt == p2m_mmio_write_dm)) are grouped together. But I am not sure if this is correct according to xen coding style. I may have misunderstood your previous comments on Sep 3, which said "the indentation would need adjustment" in reply to "[Xen-devel] [PATCH v3 1/2] x86: add p2m_mmio_write_dm"

Getting the alignment right is needed, but no via using hard tabs.

Quoted 6 lines
Quoted 6 lines

Furthermore the code you modify here suggests that p2m_ram_ro already has the needed semantics - writes get passed to the DM. None of the other changes you make, and none of the other uses of p2m_ram_ro appear to be in conflict with your intentions, so you'd really need to explain better why you need the new type.

Quoted 9 lines

Thanks Jan.
To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

Cheers,

Tim.

#5 lore raw
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Quoted 5 lines

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Jan

#6 lore raw
Quoted 32 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Quoted 5 lines

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

But yes, we ought to follow the established hypercall interface, and so we need the new type.

Quoted 8 lines
Quoted 5 lines

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Yes indeed, as P2M_RO_TYPES is defined as "must have the _PAGE_RW bit clear in their PTEs".

Cheers,

Tim.

#7 lore raw
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 13:13, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Quoted 5 lines

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Quoted 3 lines

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

Not for hvm_hap_nested_page_fault() afaict - the forwarding to DM there contradicts the "writes are discarded" model that other code paths follow.

Jan

author
#8 lore raw
Quoted 40 lines · Jan Beulich
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 13:13, <[email protected]> wrote:
Quoted 37 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Quoted 8 lines
Quoted 3 lines

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

Not for hvm_hap_nested_page_fault() afaict - the forwarding to DM there contradicts the "writes are discarded" model that other code paths follow.

Thanks, Jan.
By "inconsistency", do you mean the p2m_ram_ro shall not trigger the handle_mmio_with_translation() in hvm_hap_nested_page_fault()?
I'm also a bit confused with the "writes are discarded/dropped" comments in the code. Does this mean writes to the p2m_ram_ro pages should be abandoned without going to the dm, or going to the dm and ignored later? The code seems to be the second one.

Quoted 4 lines

Jan

#9 lore raw
Quoted 48 lines
Quoted 1 line
Quoted 1 line · On 02.12.14 at 08:48, <[email protected]> wrote:
Quoted 44 lines · Jan Beulich
Quoted 43 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 13:13, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Quoted 5 lines

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

Quoted 7 lines
Quoted 4 lines

Not for hvm_hap_nested_page_fault() afaict - the forwarding to DM there contradicts the "writes are discarded" model that other code paths follow.

Thanks, Jan.
By "inconsistency", do you mean the p2m_ram_ro shall not trigger the handle_mmio_with_translation() in hvm_hap_nested_page_fault()?

Yes, pending Tim's agreement.

Quoted 4 lines

I'm also a bit confused with the "writes are discarded/dropped" comments in the code. Does this mean writes to the p2m_ram_ro pages should be abandoned without going to the dm, or going to the dm and ignored later? The code seems to be the second one.

And it would seem to me that it should be the former.

Jan

#10 lore raw
In reply to Jan Beulich
Quoted 40 lines · Jan Beulich
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 13:13, <[email protected]> wrote:
Quoted 37 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Quoted 7 lines
Quoted 3 lines

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

Not for hvm_hap_nested_page_fault() afaict - the forwarding to DM there contradicts the "writes are discarded" model that other code paths follow.

It calls handle_mmio() to emulate the instruction for it, but handle_mmio() ought not to send anything to the DM. hvmemul_write() will call hvm_copy(), which will drop the write and report success.

The shadow code has its own emulator framework for emulting PT writes, which does much the same thing.

Tim.

#11 lore raw
Quoted 46 lines
Quoted 1 line
Quoted 1 line · On 02.12.14 at 11:37, <[email protected]> wrote:
Quoted 44 lines · Jan Beulich
Quoted 43 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 13:13, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Quoted 5 lines

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

Quoted 7 lines
Quoted 3 lines

Not for hvm_hap_nested_page_fault() afaict - the forwarding to DM there contradicts the "writes are discarded" model that other code paths follow.

It calls handle_mmio() to emulate the instruction for it, but handle_mmio() ought not to send anything to the DM. hvmemul_write() will call hvm_copy(), which will drop the write and report success.

Oh, of course - it's really just the wording of the comment there that mislead me to assume the operation gets passed on for actual handling. Thanks for clarifying!

Jan

author
#12 lore raw
In reply to Tim Deegan
Quoted 52 lines · Tim Deegan
Quoted 32 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 09:49, <[email protected]> wrote:

To my understanding, pages with p2m_ram_ro are not supposed to be modified by guest. So in __hvm_copy(), when p2m type of a page is p2m_ram_rom, no copy will occur.
However, to our usage, we just wanna this page to be write protected, so that our device model can be triggered to do some emulation. The content written to this page is supposed not to be dropped. This way, if sequentially a read operation is performed by guest to this page, the guest will still see its anticipated value.

Quoted 7 lines

__hvm_copy() is only a helper function, and doesn't write to mmio_dm space either; instead its (indirect) callers would invoke hvmemul_do_mmio() upon seeing HVMCOPY_bad_gfn_to_mfn returns. The question hence is about the apparent inconsistency resulting from writes to ram_ro being dropped here but getting passed to the DM by hvm_hap_nested_page_fault(). Tim - is that really intentional?

No - and AFAICT it shouldn't be happening. It _is_ how it was implemented originally, because it involved fewer moving parts and didn't need to be efficient (and after all, writes to entirely missing addresses go to the device model too).

Quoted 6 lines

But the code was later updated to log and discard writes to read-only memory (see 4d8aa29 from Trolle Selander).

Early version of p2m_ram_ro were documented in the internal headers as sending the writes to the DM, but the public interface (HVMMEM_ram_ro) has always said that writes are discarded.

Quoted 5 lines

Hmm, so which way do you recommend resolving the inconsistency then - match what the public interface says or what the apparent original intention for the internal type was? Presumably we need to follow the public interface mandated model, and hence require the new type to be introduced.

Sorry, I was unclear -- there isn't an inconsistency; both internal and public headers currently say that writes are discarded and AFAICT that is what the code does.

But yes, we ought to follow the established hypercall interface, and so we need the new type.

Quoted 6 lines
Quoted 5 lines

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

Quoted 6 lines
Quoted 2 lines

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Yes indeed, as P2M_RO_TYPES is defined as "must have the _PAGE_RW bit clear in their PTEs".

Thanks Tim.
Following are my understanding of the P2M_RO_TYPES and your comments.
Not sure if I get it right. Please correct me if anything wrong:
1> The P2M_RO_TYPES now bears 2 meanings: one is "w bit is clear in the pte"; and another being to discard the write operations; 2> We better define another class to bear the second meaning.

Also some questions for the new p2m class, say P2M_DISCARD_WRITE_TYPES, and the new predicates, say p2m_is_discard_write:
1> You mentioned emulate_gva_to_mfn() and __hvm_copy() should discard the write ops, yet I also noticed many other places using the p2m_is_readonly, or only the "p2mt == p2m_ram_ro" judgement(in __hvm_copy/__hvm_clear). Among all these other places, is there any ones also supposed to use the p2m_is_discard_write? 2> p2m_grant_map_ro is also supposed to be discarded? Will handling of this type of pages goes into __hvm_copy()/__hvm_clear(), or should?

I'm a new guy of this area, and sorry for my messed questions. :)

Yu

Quoted 5 lines

Cheers,

Tim.

#13 lore raw
Quoted 16 lines · Zhang
Quoted 8 lines · Tim Deegan
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

Quoted 6 lines
Quoted 2 lines

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Yes indeed, as P2M_RO_TYPES is defined as "must have the _PAGE_RW bit clear in their PTEs".

Quoted 6 lines

Thanks Tim.
Following are my understanding of the P2M_RO_TYPES and your comments. Not sure if I get it right. Please correct me if anything wrong:
1> The P2M_RO_TYPES now bears 2 meanings: one is "w bit is clear in the pte"; and another being to discard the write operations;
2> We better define another class to bear the second meaning.

Yes, that's what I meant.

Answering your other questions in reverse order:

Quoted 2 lines

2> p2m_grant_map_ro is also supposed to be discarded? Will handling of this type of pages goes into __hvm_copy()/__hvm_clear(), or should?

I think so, yes. At the moment we inject #GP when the guest writes to a read-only grant, which is OK: the guest really ought to know better. But I think we'll probably end up with neater code if we handle read-only grants the same way as p2m_ram_ro.

Anyone else have an opinion on the right thing to do here?

Quoted 7 lines

Also some questions for the new p2m class, say P2M_DISCARD_WRITE_TYPES, and the new predicates, say p2m_is_discard_write:
1> You mentioned emulate_gva_to_mfn() and __hvm_copy() should discard the write ops, yet I also noticed many other places using the p2m_is_readonly, or only the "p2mt == p2m_ram_ro" judgement(in __hvm_copy/__hvm_clear). Among all these other places, is there any ones also supposed to use the p2m_is_discard_write?

I've just had a look through them all, and I can see exactly four places that should be using the new p2m_is_discard_write() test:

- emulate_gva_to_mfn() (Though in fact it's a no-op as shadow-mode guests never have p2m_ram_shared or p2m_ram_logdirty mappings.)
- __hvm_copy()
- __hvm_clear() and
- hvm_hap_nested_page_fault() (where you should also remove the explicit handling of p2m_grant_map_ro below.)

Looking through that turned up a few other oddities, which I'm listing here to remind myself to look at them later (i.e. you don't need to worry about them for this patch):

- nsvm_get_nvmcb_page() and nestedhap_walk_L0_p2m() need to handle p2m_ram_logdirty or they might spuriously fail duiring live migration.
- __hvm_copy() and __hvm_clear are probably over-strict in their failure to handle grant types.
- P2M_UNMAP_TYPES in vmce.c is a mess. It's not the right place to define this, since it definitely won't be seen my anyone adding a new type, and it already has an 'XXX' comment that says it doesn't cover a lot of cases. :(

I'll have a look at those another time.

Cheers,

Tim.

#14 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 02.12.14 at 12:40, <[email protected]> wrote:
Quoted 2 lines · Zhang

2> p2m_grant_map_ro is also supposed to be discarded? Will handling of this type of pages goes into __hvm_copy()/__hvm_clear(), or should?

I think so, yes. At the moment we inject #GP when the guest writes to a read-only grant, which is OK: the guest really ought to know better. But I think we'll probably end up with neater code if we handle read-only grants the same way as p2m_ram_ro.

Anyone else have an opinion on the right thing to do here?

I actually came to the same conclusion.

Jan

author
#15 lore raw
In reply to Tim Deegan
Quoted 49 lines · Tim Deegan
Quoted 16 lines · Zhang
Quoted 8 lines · Tim Deegan
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

Quoted 6 lines
Quoted 2 lines

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Yes indeed, as P2M_RO_TYPES is defined as "must have the _PAGE_RW bit clear in their PTEs".

Quoted 6 lines

Thanks Tim.
Following are my understanding of the P2M_RO_TYPES and your comments. Not sure if I get it right. Please correct me if anything wrong:
1> The P2M_RO_TYPES now bears 2 meanings: one is "w bit is clear in the pte"; and another being to discard the write operations;
2> We better define another class to bear the second meaning.

Yes, that's what I meant.

Answering your other questions in reverse order:

Quoted 2 lines

2> p2m_grant_map_ro is also supposed to be discarded? Will handling of this type of pages goes into __hvm_copy()/__hvm_clear(), or should?

I think so, yes. At the moment we inject #GP when the guest writes to a read-only grant, which is OK: the guest really ought to know better. But I think we'll probably end up with neater code if we handle read-only grants the same way as p2m_ram_ro.

Anyone else have an opinion on the right thing to do here?

Quoted 7 lines

Also some questions for the new p2m class, say P2M_DISCARD_WRITE_TYPES, and the new predicates, say p2m_is_discard_write:
1> You mentioned emulate_gva_to_mfn() and __hvm_copy() should discard the write ops, yet I also noticed many other places using the p2m_is_readonly, or only the "p2mt == p2m_ram_ro" judgement(in __hvm_copy/__hvm_clear). Among all these other places, is there any ones also supposed to use the p2m_is_discard_write?

I've just had a look through them all, and I can see exactly four places that should be using the new p2m_is_discard_write() test:

Quoted 7 lines

- emulate_gva_to_mfn() (Though in fact it's a no-op as shadow-mode guests never have p2m_ram_shared or p2m_ram_logdirty mappings.)
- __hvm_copy()
- __hvm_clear() and
- hvm_hap_nested_page_fault() (where you should also remove the explicit handling of p2m_grant_map_ro below.)

Thank you, Tim & Jan.
To give a summary for all the comments:

1> new p2m type p2m_mmio_write_dm is to be added;
2> new p2m type need to be added to P2M_RO_TYPES class;
3> new p2m class, say P2M_DISCARD_WRITE_TYPES(which only include p2m_ram_ro and p2m_grant_map_ro), and the new predicates, say p2m_is_discard_write are needed to in these 4 places to discard the write op; 4> and of cause hvm_hap_nested_page_fault() do not need the special handling for p2m_grant_map_ro anymore;
5> coding style changes pointed out by Jan
6> clear the commit message

I'll prepare the patch and thanks! :)

Yu

Quoted 21 lines

Looking through that turned up a few other oddities, which I'm listing here to remind myself to look at them later (i.e. you don't need to worry about them for this patch):

- nsvm_get_nvmcb_page() and nestedhap_walk_L0_p2m() need to handle p2m_ram_logdirty or they might spuriously fail duiring live migration.
- __hvm_copy() and __hvm_clear are probably over-strict in their failure to handle grant types.
- P2M_UNMAP_TYPES in vmce.c is a mess. It's not the right place to define this, since it definitely won't be seen my anyone adding a new type, and it already has an 'XXX' comment that says it doesn't cover a lot of cases. :(

I'll have a look at those another time.

Cheers,

Tim.

author
#16 lore raw
In reply to Tim Deegan
Quoted 60 lines · Tim Deegan
Quoted 16 lines · Zhang
Quoted 8 lines · Tim Deegan
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 01.12.14 at 11:30, <[email protected]> wrote:

During this bit of archaeology I realised that either this new type should _not_ be made part of P2M_RO_TYPES, or, better, we need a new class of P2M types (P2M_DISCARD_WRITE_TYPES, say) that should be used for these paths in emulate_gva_to_mfn() and __hvm_copy(), containing just p2m_ram_ro and p2m_grant_map_ro.

Quoted 6 lines
Quoted 2 lines

And I suppose in that latter case the new type could be made part of P2M_RO_TYPES()?

Yes indeed, as P2M_RO_TYPES is defined as "must have the _PAGE_RW bit clear in their PTEs".

Quoted 6 lines

Thanks Tim.
Following are my understanding of the P2M_RO_TYPES and your comments. Not sure if I get it right. Please correct me if anything wrong:
1> The P2M_RO_TYPES now bears 2 meanings: one is "w bit is clear in the pte"; and another being to discard the write operations;
2> We better define another class to bear the second meaning.

Yes, that's what I meant.

Answering your other questions in reverse order:

Quoted 2 lines

2> p2m_grant_map_ro is also supposed to be discarded? Will handling of this type of pages goes into __hvm_copy()/__hvm_clear(), or should?

I think so, yes. At the moment we inject #GP when the guest writes to a read-only grant, which is OK: the guest really ought to know better. But I think we'll probably end up with neater code if we handle read-only grants the same way as p2m_ram_ro.

Anyone else have an opinion on the right thing to do here?

Quoted 7 lines

Also some questions for the new p2m class, say P2M_DISCARD_WRITE_TYPES, and the new predicates, say p2m_is_discard_write:
1> You mentioned emulate_gva_to_mfn() and __hvm_copy() should discard the write ops, yet I also noticed many other places using the p2m_is_readonly, or only the "p2mt == p2m_ram_ro" judgement(in __hvm_copy/__hvm_clear). Among all these other places, is there any ones also supposed to use the p2m_is_discard_write?

I've just had a look through them all, and I can see exactly four places that should be using the new p2m_is_discard_write() test:

- emulate_gva_to_mfn() (Though in fact it's a no-op as shadow-mode guests never have p2m_ram_shared or p2m_ram_logdirty mappings.)
- __hvm_copy()
- __hvm_clear() and
- hvm_hap_nested_page_fault() (where you should also remove the explicit handling of p2m_grant_map_ro below.)

Looking through that turned up a few other oddities, which I'm listing here to remind myself to look at them later (i.e. you don't need to worry about them for this patch):

Quoted 5 lines

- nsvm_get_nvmcb_page() and nestedhap_walk_L0_p2m() need to handle p2m_ram_logdirty or they might spuriously fail duiring live migration.
- __hvm_copy() and __hvm_clear are probably over-strict in their failure to handle grant types.

Hi Tim. Sorry to bother you. :)
I just noticed that in __hvm_copy()/__hvm_clear(), the grant types are handled before the p2m_ram_ro - will return HVMCOPY_unhandleable. So if p2m_is_discard_write() is supposed to replace the handling of p2m_ram_ro, handling of p2m_grant_map_ro will still return HVMCOPY_unhandleable, before the p2m_is_discard_write() predicate. Even we move the testing of p2m_is_discard_write() before the handling of grant types, it seems not quite clean.
By "over-strict in their failure to handle grant types.", do you also mean this?

Thanks
Yu

Quoted 17 lines

- P2M_UNMAP_TYPES in vmce.c is a mess. It's not the right place to define this, since it definitely won't be seen my anyone adding a new type, and it already has an 'XXX' comment that says it doesn't cover a lot of cases. :(

I'll have a look at those another time.

Cheers,

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Hi,

Quoted 9 lines · Zhang

I just noticed that in __hvm_copy()/__hvm_clear(), the grant types are handled before the p2m_ram_ro - will return HVMCOPY_unhandleable. So if p2m_is_discard_write() is supposed to replace the handling of p2m_ram_ro, handling of p2m_grant_map_ro will still return HVMCOPY_unhandleable, before the p2m_is_discard_write() predicate. Even we move the testing of p2m_is_discard_write() before the handling of grant types, it seems not quite clean.
By "over-strict in their failure to handle grant types.", do you also mean this?

Yes, that's the sort of thing I meant. I'll try to write a patch for that later today or next week -- in the meantime I think you should ignore it. :)

An unrelated thought: when you send your next version can you send it as a two-patch series, where the first patch does the p2m_is_discard_write() changes and the second adds your new type?

Cheers,

Tim.

author
#18 lore raw
Quoted 13 lines · Tim Deegan

Hi,

Quoted 9 lines · Zhang

I just noticed that in __hvm_copy()/__hvm_clear(), the grant types are handled before the p2m_ram_ro - will return HVMCOPY_unhandleable. So if p2m_is_discard_write() is supposed to replace the handling of p2m_ram_ro, handling of p2m_grant_map_ro will still return HVMCOPY_unhandleable, before the p2m_is_discard_write() predicate. Even we move the testing of p2m_is_discard_write() before the handling of grant types, it seems not quite clean.
By "over-strict in their failure to handle grant types.", do you also mean this?

Quoted 7 lines

Yes, that's the sort of thing I meant. I'll try to write a patch for that later today or next week -- in the meantime I think you should ignore it. :)

An unrelated thought: when you send your next version can you send it as a two-patch series, where the first patch does the p2m_is_discard_write() changes and the second adds your new type?

Sure, and thank you, Tim. :)

Quoted 11 lines

Cheers,

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel