Skip to contentSkip to conversation

x86/ioreq server: Introduce HVMMEM_ioreq_server mem type.

PATCH Superseded Yu Zhang 6 participants 46 messages Apr 2, 2017 – Apr 6, 2017

v10 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

v10 cover x86/ioreq server: Introduce HVMMEM_ioreq_server mem type.

XenGT leverages ioreq server to track and forward the accesses to GPU I/O resources, e.g. the PPGTT(per-process graphic translation tables). Currently, ioreq server uses rangeset to track the BDF/ PIO/MMIO ranges to be emulated. To select an ioreq server, the rangeset is searched to see if the I/O range is recorded. However, number of ram pages to be tracked may exceed the upper limit of rangeset.

Previously, one solution was proposed to refactor the rangeset, and extend its upper limit. However, after 12 rounds discussion, we have decided to drop this approach due to security concerns. Now this new patch series introduces a new mem type, HVMMEM_ioreq_server, and added hvm operations to let one ioreq server to claim its ownership of ram pages with this type. Accesses to a page of this type will be handled by the specified ioreq server directly.

Yu Zhang (6):
x86/ioreq server: Release the p2m lock after mmio is handled. x86/ioreq server: Add DMOP to map guest ram with p2m_ioreq_server to
an ioreq server.
x86/ioreq server: Add device model wrappers for new DMOP x86/ioreq server: Handle read-modify-write cases for p2m_ioreq_server
pages.
x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server
entries.
x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server
entries when an ioreq server unmaps.

 tools/libs/devicemodel/core.c                   |  25 +++++
 tools/libs/devicemodel/include/xendevicemodel.h |  18 ++++
 tools/libs/devicemodel/libxendevicemodel.map    |   1 +
 tools/libxc/include/xenctrl_compat.h            |   5 +
 tools/libxc/xc_devicemodel_compat.c             |  17 ++++
 xen/arch/x86/hvm/dm.c                           |  70 +++++++++++++-
 xen/arch/x86/hvm/emulate.c                      |  95 ++++++++++++++++++-
 xen/arch/x86/hvm/hvm.c                          |   7 +-
 xen/arch/x86/hvm/ioreq.c                        |  52 +++++++++++
 xen/arch/x86/mm/hap/hap.c                       |   9 ++
 xen/arch/x86/mm/p2m-ept.c                       |  16 +++-
 xen/arch/x86/mm/p2m-pt.c                        |  32 +++++--
 xen/arch/x86/mm/p2m.c                           | 116 ++++++++++++++++++++++++
 xen/arch/x86/mm/shadow/multi.c                  |   3 +-
 xen/include/asm-x86/hvm/ioreq.h                 |   2 +
 xen/include/asm-x86/p2m.h                       |  40 +++++++-
 xen/include/public/hvm/dm_op.h                  |  28 ++++++
 xen/include/public/hvm/hvm_op.h                 |   8 +-
 18 files changed, 515 insertions(+), 29 deletions(-)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH v10 1/6 x86/ioreq server: Release the p2m lock after mmio is handled.

Routine hvmemul_do_io() may need to peek the p2m type of a gfn to select the ioreq server. For example, operations on gfns with p2m_ioreq_server type will be delivered to a corresponding ioreq server, and this requires that the p2m type not be switched back to p2m_ram_rw during the emulation process. To avoid this race condition, we delay the release of p2m lock in hvm_hap_nested_page_fault() until mmio is handled.

Note: previously in hvm_hap_nested_page_fault(), put_gfn() was moved before the handling of mmio, due to a deadlock risk between the p2m lock and the event lock(in commit 77b8dfe). Later, a per-event channel lock was introduced in commit de6acb7, to send events. So we do not need to worry about the deadlock issue.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

changes in v4:
- According to comments from Jan: remove the redundant "rc = 0" code.

---
 xen/arch/x86/hvm/hvm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -6
index 0282986..bd18d8e 100644
@@ -1815,15 +1815,10 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
          (npfec.write_access &&
           (p2m_is_discard_write(p2mt) || (p2mt == p2m_ioreq_server))) )
     {
-        __put_gfn(p2m, gfn);
-        if ( ap2m_active )
-            __put_gfn(hostp2m, gfn);
-
-        rc = 0;
         if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) )
             hvm_inject_hw_exception(TRAP_gp_fault, 0);
         rc = 1;
-        goto out;
+        goto out_put_gfn;
     }
 
     /* Check if the page has been paged out */
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#2 lore raw

PATCH v10 2/6 x86/ioreq server: Add DMOP to map guest ram with p2m_ioreq_server to an ioreq server.

In reply to Yu Zhang

Previously, p2m_ioreq_server is used to write-protect guest ram pages, which are tracked with ioreq server's rangeset. However, number of ram pages to be tracked may exceed the upper limit of rangeset.

Now, a new DMOP - XEN_DMOP_map_mem_type_to_ioreq_server, is added to let one ioreq server claim/disclaim its responsibility for the handling of guest pages with p2m type p2m_ioreq_server. Users of this DMOP can specify which kind of operation is supposed to be emulated in a parameter named flags. Currently, this DMOP only support the emulation of write operations. And it can be further extended to support the emulation of read ones if an ioreq server has such requirement in the future.

For now, we only support one ioreq server for this p2m type, so once an ioreq server has claimed its ownership, subsequent calls of the XEN_DMOP_map_mem_type_to_ioreq_server will fail. Users can also disclaim the ownership of guest ram pages with p2m_ioreq_server, by triggering this new DMOP, with ioreq server id set to the current owner's and flags parameter set to 0.

Note:
a> both XEN_DMOP_map_mem_type_to_ioreq_server and p2m_ioreq_server are only supported for HVMs with HAP enabled.

b> only after one ioreq server claims its ownership of p2m_ioreq_server, will the p2m type change to p2m_ioreq_server be allowed.

c> this patch shall be accepted together with the following ones in this series.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>
Cc: Tim Deegan <[email protected]>

changes in v10:
- According to comments from Jan: add a new patch for the libdevicemodel and libxc interface.
- According to comments from Jan: remove p2m_destroy_ioreq_server(), use

    p2m_set_ioreq_server(d, 0, s) instead.

- According to comments from Jan & Kevin: comments changes in hvmemul_do_io().
- According to comments from Jan & Kevin: commit message changes.

changes in v8:
- According to comments from Jan & Paul: comments changes in hvmemul_do_io().
- According to comments from Jan: remove the redundant code which would only be useful for read emulations.
- According to comments from Jan: change interface which maps mem type to ioreq server, removed uint16_t pad and added an uint64_t opaque.
- Address other comments from Jan, i.e. correct return values; remove stray cast.

changes in v7:
- Use new ioreq server interface - XEN_DMOP_map_mem_type_to_ioreq_server.
- According to comments from George: removed domain_pause/unpause() in hvm_map_mem_type_to_ioreq_server(), because it's too expensive, and we can avoid the:
a> deadlock issue existed in v6 patch, between p2m lock and ioreq server
lock by using these locks in the same order - solved in patch 4;
b> for race condition between vm exit and ioreq server unbinding, we can
just retry this instruction.
- According to comments from Jan and George: continue to clarify logic in

    hvmemul_do_io().

- According to comments from Jan: clarify comment in p2m_set_ioreq_server().

changes in v6:
- Clarify logic in hvmemul_do_io().
- Use recursive lock for ioreq server lock.
- Remove debug print when mapping ioreq server.
- Clarify code in ept_p2m_type_to_flags() for consistency.
- Remove definition of P2M_IOREQ_HANDLE_WRITE_ACCESS.
- Add comments for HVMMEM_ioreq_server to note only changes to/from HVMMEM_ram_rw are permitted.
- Add domain_pause/unpause() in hvm_map_mem_type_to_ioreq_server() to avoid the race condition when a vm exit happens on a write- protected page, just to find the ioreq server has been unmapped already.
- Introduce a seperate patch to delay the release of p2m lock to avoid the race condition.
- Introduce a seperate patch to handle the read-modify-write operations on a write protected page.

changes in v5:
- Simplify logic in hvmemul_do_io().
- Use natual width types instead of fixed width types when possible.
- Do not grant executable permission for p2m_ioreq_server entries.
- Clarify comments and commit message.
- Introduce a seperate patch to recalculate the p2m types after the ioreq server unmaps the p2m_ioreq_server.
changes in v4:
- According to Paul's advice, add comments around the definition of HVMMEM_iore_server in hvm_op.h.
- According to Wei Liu's comments, change the format of the commit message.

changes in v3:
- Only support write emulation in this patch;
- Remove the code to handle race condition in hvmemul_do_io(),
- No need to reset the p2m type after an ioreq server has disclaimed

    its ownership of p2m_ioreq_server;
  - Only allow p2m type change to p2m_ioreq_server after an ioreq
    server has claimed its ownership of p2m_ioreq_server;
  - Only allow p2m type change to p2m_ioreq_server from pages with type
    p2m_ram_rw, and vice versa;
  - HVMOP_map_mem_type_to_ioreq_server interface change - use uint16,
    instead of enum to specify the memory type;

- Function prototype change to p2m_get_ioreq_server();
- Coding style changes;
- Commit message changes;
- Add Tim's Acked-by.

changes in v2:
- Only support HAP enabled HVMs;
- Replace p2m_mem_type_changed() with p2m_change_entry_type_global() to reset the p2m type, when an ioreq server tries to claim/disclaim its ownership of p2m_ioreq_server;
- Comments changes.

---
 xen/arch/x86/hvm/dm.c           | 35 +++++++++++++++++++++--
 xen/arch/x86/hvm/emulate.c      | 61 ++++++++++++++++++++++++++++++++++++++---
 xen/arch/x86/hvm/ioreq.c        | 44 +++++++++++++++++++++++++++++
 xen/arch/x86/mm/p2m-ept.c       |  8 +++++-
 xen/arch/x86/mm/p2m-pt.c        | 19 +++++++++----
 xen/arch/x86/mm/p2m.c           | 58 +++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/mm/shadow/multi.c  |  3 +-
 xen/include/asm-x86/hvm/ioreq.h |  2 ++
 xen/include/asm-x86/p2m.h       | 25 +++++++++++++++--
 xen/include/public/hvm/dm_op.h  | 28 +++++++++++++++++++
 xen/include/public/hvm/hvm_op.h |  8 +++++-
 11 files changed, 272 insertions(+), 19 deletions(-)
xen/arch/x86/hvm/dm.c +33 -2
index 333c884..7e0da81 100644
@@ -173,9 +173,14 @@ static int modified_memory(struct domain *d,
 
 static bool allow_p2m_type_change(p2m_type_t old, p2m_type_t new)
 {
+    if ( new == p2m_ioreq_server )
+        return old == p2m_ram_rw;
+
+    if ( old == p2m_ioreq_server )
+        return new == p2m_ram_rw;
+
     return p2m_is_ram(old) ||
-           (p2m_is_hole(old) && new == p2m_mmio_dm) ||
-           (old == p2m_ioreq_server && new == p2m_ram_rw);
+           (p2m_is_hole(old) && new == p2m_mmio_dm);
 }
 
 static int set_mem_type(struct domain *d,
@@ -202,6 +207,18 @@ static int set_mem_type(struct domain *d,
          unlikely(data->mem_type == HVMMEM_unused) )
         return -EINVAL;
 
+    if ( data->mem_type  == HVMMEM_ioreq_server )
+    {
+        unsigned int flags;
+
+        if ( !hap_enabled(d) )
+            return -EOPNOTSUPP;
+
+        /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
+        if ( !p2m_get_ioreq_server(d, &flags) )
+            return -EINVAL;
+    }
+
     while ( iter < data->nr )
     {
         unsigned long pfn = data->first_pfn + iter;
@@ -365,6 +382,20 @@ static int dm_op(domid_t domid,
         break;
     }
 
+    case XEN_DMOP_map_mem_type_to_ioreq_server:
+    {
+        const struct xen_dm_op_map_mem_type_to_ioreq_server *data =
+            &op.u.map_mem_type_to_ioreq_server;
+
+        rc = -EOPNOTSUPP;
+        if ( !hap_enabled(d) )
+            break;
+
+        rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
+                                              data->type, data->flags);
+        break;
+    }
+
     case XEN_DMOP_set_ioreq_server_state:
     {
         const struct xen_dm_op_set_ioreq_server_state *data =
xen/arch/x86/hvm/emulate.c +57 -4

85 lines not on this page · raw message

xen/arch/x86/hvm/ioreq.c +44 -0

56 lines not on this page · raw message

xen/arch/x86/mm/p2m-ept.c +7 -1
index 568944f..cc1eb21 100644
@@ -131,6 +131,13 @@ static void ept_p2m_type_to_flags(struct p2m_domain *p2m, ept_entry_t *entry,
             entry->r = entry->w = entry->x = 1;
             entry->a = entry->d = !!cpu_has_vmx_ept_ad;
             break;
+        case p2m_ioreq_server:
+            entry->r = 1;
+            entry->w = !(p2m->ioreq.flags & XEN_DMOP_IOREQ_MEM_ACCESS_WRITE);
+            entry->x = 0;
+            entry->a = !!cpu_has_vmx_ept_ad;
+            entry->d = entry->w && entry->a;
+            break;
         case p2m_mmio_direct:
             entry->r = entry->x = 1;
             entry->w = !rangeset_contains_singleton(mmio_ro_ranges,
@@ -170,7 +177,6 @@ static void ept_p2m_type_to_flags(struct p2m_domain *p2m, ept_entry_t *entry,
             entry->a = entry->d = !!cpu_has_vmx_ept_ad;
             break;
         case p2m_grant_map_ro:
-        case p2m_ioreq_server:
             entry->r = 1;
             entry->w = entry->x = 0;
             entry->a = !!cpu_has_vmx_ept_ad;
xen/arch/x86/mm/p2m-pt.c +13 -6
index 268b232..c0055f3 100644
@@ -70,7 +70,9 @@ static const unsigned long pgt[] = {
     PGT_l3_page_table
 };
 
-static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn,
+static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m,
+                                       p2m_type_t t,
+                                       mfn_t mfn,
                                        unsigned int level)
 {
     unsigned long flags;
@@ -92,8 +94,12 @@ 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_ioreq_server:
         return flags | P2M_BASE_FLAGS | _PAGE_NX_BIT;
+    case p2m_ioreq_server:
+        flags |= P2M_BASE_FLAGS | _PAGE_RW | _PAGE_NX_BIT;
+        if ( p2m->ioreq.flags & XEN_DMOP_IOREQ_MEM_ACCESS_WRITE )
+            return flags & ~_PAGE_RW;
+        return flags;
     case p2m_ram_ro:
     case p2m_ram_logdirty:
     case p2m_ram_shared:
@@ -440,7 +446,8 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn)
             p2m_type_t p2mt = p2m_is_logdirty_range(p2m, gfn & mask, gfn | ~mask)
                               ? p2m_ram_logdirty : p2m_ram_rw;
             unsigned long mfn = l1e_get_pfn(e);
-            unsigned long flags = p2m_type_to_flags(p2mt, _mfn(mfn), level);
+            unsigned long flags = p2m_type_to_flags(p2m, p2mt,
+                                                    _mfn(mfn), level);
 
             if ( level )
             {
@@ -578,7 +585,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         ASSERT(!mfn_valid(mfn) || p2mt != p2m_mmio_direct);
         l3e_content = mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt)
             ? l3e_from_pfn(mfn_x(mfn),
-                           p2m_type_to_flags(p2mt, mfn, 2) | _PAGE_PSE)
+                           p2m_type_to_flags(p2m, p2mt, mfn, 2) | _PAGE_PSE)
             : l3e_empty();
         entry_content.l1 = l3e_content.l3;
 
@@ -615,7 +622,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
 
         if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )
             entry_content = p2m_l1e_from_pfn(mfn_x(mfn),
-                                             p2m_type_to_flags(p2mt, mfn, 0));
+                                         p2m_type_to_flags(p2m, p2mt, mfn, 0));
         else
             entry_content = l1e_empty();
 
@@ -652,7 +659,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         ASSERT(!mfn_valid(mfn) || p2mt != p2m_mmio_direct);
         if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) )
             l2e_content = l2e_from_pfn(mfn_x(mfn),
-                                       p2m_type_to_flags(p2mt, mfn, 1) |
+                                       p2m_type_to_flags(p2m, p2mt, mfn, 1) |
                                        _PAGE_PSE);
         else
             l2e_content = l2e_empty();
xen/arch/x86/mm/p2m.c +58 -0

70 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +1 -2
index 4798c93..5195d61 100644
@@ -3306,8 +3306,7 @@ static int sh_page_fault(struct vcpu *v,
     }
 
     /* Need to hand off device-model MMIO to the device model */
-    if ( p2mt == p2m_mmio_dm
-         || (p2mt == p2m_ioreq_server && ft == ft_demand_write) )
+    if ( p2mt == p2m_mmio_dm )
     {
         gpa = guest_walk_to_gpa(&gw);
         goto mmio;
xen/include/asm-x86/hvm/ioreq.h +2 -0
index fbf2c74..b43667a 100644
@@ -37,6 +37,8 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
 int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
                                          uint32_t type, uint64_t start,
                                          uint64_t end);
+int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
+                                     uint32_t type, uint32_t flags);
 int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id,
                                bool_t enabled);
 
xen/include/asm-x86/p2m.h +22 -3
index bc189d1..4521620 100644
@@ -89,7 +89,8 @@ typedef unsigned int p2m_query_t;
                        | p2m_to_mask(p2m_ram_paging_out)      \
                        | p2m_to_mask(p2m_ram_paged)           \
                        | p2m_to_mask(p2m_ram_paging_in)       \
-                       | p2m_to_mask(p2m_ram_shared))
+                       | p2m_to_mask(p2m_ram_shared)          \
+                       | p2m_to_mask(p2m_ioreq_server))
 
 /* Types that represent a physmap hole that is ok to replace with a shared
  * entry */
@@ -111,8 +112,7 @@ typedef unsigned int p2m_query_t;
 #define P2M_RO_TYPES (p2m_to_mask(p2m_ram_logdirty)     \
                       | p2m_to_mask(p2m_ram_ro)         \
                       | p2m_to_mask(p2m_grant_map_ro)   \
-                      | p2m_to_mask(p2m_ram_shared)     \
-                      | p2m_to_mask(p2m_ioreq_server))
+                      | p2m_to_mask(p2m_ram_shared))
 
 /* Write-discard types, which should discard the write operations */
 #define P2M_DISCARD_WRITE_TYPES (p2m_to_mask(p2m_ram_ro)     \
@@ -336,6 +336,20 @@ struct p2m_domain {
         struct ept_data ept;
         /* NPT-equivalent structure could be added here. */
     };
+
+     struct {
+         spinlock_t lock;
+         /*
+          * ioreq server who's responsible for the emulation of
+          * gfns with specific p2m type(for now, p2m_ioreq_server).
+          */
+         struct hvm_ioreq_server *server;
+         /*
+          * flags specifies whether read, write or both operations
+          * are to be emulated by an ioreq server.
+          */
+         unsigned int flags;
+     } ioreq;
 };
 
 /* get host p2m table */
@@ -827,6 +841,11 @@ static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn)
     return flags;
 }
 
+int p2m_set_ioreq_server(struct domain *d, unsigned int flags,
+                         struct hvm_ioreq_server *s);
+struct hvm_ioreq_server *p2m_get_ioreq_server(struct domain *d,
+                                              unsigned int *flags);
+
 #endif /* _XEN_ASM_X86_P2M_H */
 
 /*
xen/include/public/hvm/dm_op.h +28 -0
index f54cece..5ea79ef 100644
@@ -318,6 +318,32 @@ struct xen_dm_op_inject_msi {
     uint64_aligned_t addr;
 };
 
+/*
+ * XEN_DMOP_map_mem_type_to_ioreq_server : map or unmap the IOREQ Server <id>
+ *                                      to specific memory type <type>
+ *                                      for specific accesses <flags>
+ *
+ * For now, flags only accept the value of XEN_DMOP_IOREQ_MEM_ACCESS_WRITE,
+ * which means only write operations are to be forwarded to an ioreq server.
+ * Support for the emulation of read operations can be added when an ioreq
+ * server has such requirement in future.
+ */
+#define XEN_DMOP_map_mem_type_to_ioreq_server 15
+
+struct xen_dm_op_map_mem_type_to_ioreq_server {
+    ioservid_t id;      /* IN - ioreq server id */
+    uint16_t type;      /* IN - memory type */
+    uint32_t flags;     /* IN - types of accesses to be forwarded to the
+                           ioreq server. flags with 0 means to unmap the
+                           ioreq server */
+
+#define XEN_DMOP_IOREQ_MEM_ACCESS_READ (1u << 0)
+#define XEN_DMOP_IOREQ_MEM_ACCESS_WRITE (1u << 1)
+
+    uint64_t opaque;    /* IN/OUT - only used for hypercall continuation,
+                           has to be set to zero by the caller */
+};
+
 struct xen_dm_op {
     uint32_t op;
     uint32_t pad;
@@ -336,6 +362,8 @@ struct xen_dm_op {
         struct xen_dm_op_set_mem_type set_mem_type;
         struct xen_dm_op_inject_event inject_event;
         struct xen_dm_op_inject_msi inject_msi;
+        struct xen_dm_op_map_mem_type_to_ioreq_server
+                map_mem_type_to_ioreq_server;
     } u;
 };
 
xen/include/public/hvm/hvm_op.h +7 -1
index bc00ef0..0bdafdf 100644
@@ -93,7 +93,13 @@ typedef enum {
     HVMMEM_unused,             /* Placeholder; setting memory to this type
                                   will fail for code after 4.7.0 */
 #endif
-    HVMMEM_ioreq_server
+    HVMMEM_ioreq_server        /* Memory type claimed by an ioreq server; type
+                                  changes to this value are only allowed after
+                                  an ioreq server has claimed its ownership.
+                                  Only pages with HVMMEM_ram_rw are allowed to
+                                  change to this type; conversely, pages with
+                                  this type are only allowed to be changed back
+                                  to HVMMEM_ram_rw. */
 } hvmmem_type_t;
 
 /* Hint from PV drivers for pagetable destruction. */
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#5 lore raw

PATCH v10 3/6 x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/libs/devicemodel/core.c                   | 25 +++++++++++++++++++++++++
 tools/libs/devicemodel/include/xendevicemodel.h | 18 ++++++++++++++++++
 tools/libs/devicemodel/libxendevicemodel.map    |  1 +
 tools/libxc/include/xenctrl_compat.h            |  5 +++++
 tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
 5 files changed, 66 insertions(+)
tools/libs/devicemodel/core.c +25 -0
index a85cb49..ff09819 100644
@@ -244,6 +244,31 @@ int xendevicemodel_unmap_io_range_from_ioreq_server(
     return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));
 }
 
+int xendevicemodel_map_mem_type_to_ioreq_server(
+    xendevicemodel_handle *dmod, domid_t domid, ioservid_t id, uint16_t type,
+    uint32_t flags)
+{
+    struct xen_dm_op op;
+    struct xen_dm_op_map_mem_type_to_ioreq_server *data;
+
+    if (type != HVMMEM_ioreq_server ||
+        flags & ~XEN_DMOP_IOREQ_MEM_ACCESS_WRITE) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    memset(&op, 0, sizeof(op));
+
+    op.op = XEN_DMOP_map_mem_type_to_ioreq_server;
+    data = &op.u.map_mem_type_to_ioreq_server;
+
+    data->id = id;
+    data->type = type;
+    data->flags = flags;
+
+    return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));
+}
+
 int xendevicemodel_map_pcidev_to_ioreq_server(
     xendevicemodel_handle *dmod, domid_t domid, ioservid_t id,
     uint16_t segment, uint8_t bus, uint8_t device, uint8_t function)
tools/libs/devicemodel/include/xendevicemodel.h +18 -0
index b3f600e..1da216f 100644
@@ -104,6 +104,24 @@ int xendevicemodel_unmap_io_range_from_ioreq_server(
     uint64_t start, uint64_t end);
 
 /**
+ * This function registers/deregisters a memory type for emulation.
+ *
+ * @parm dmod a handle to an open devicemodel interface.
+ * @parm domid the domain id to be serviced.
+ * @parm id the IOREQ Server id.
+ * @parm type the memory type to be emulated. For now, only HVMMEM_ioreq_server
+ *            is supported, and in the future new types can be introduced, e.g.
+ *            HVMMEM_ioreq_serverX mapped to ioreq server X.
+ * @parm flags operations to be emulated; 0 for unmap. For now, only write
+ *             operations will be emulated and can be extended to emulate
+ *             read ones in the future.
+ * @return 0 on success, -1 on failure.
+ */
+int xendevicemodel_map_mem_type_to_ioreq_server(
+    xendevicemodel_handle *dmod, domid_t domid, ioservid_t id, uint16_t type,
+    uint32_t flags);
+
+/**
  * This function registers a PCI device for config space emulation.
  *
  * @parm dmod a handle to an open devicemodel interface.
tools/libs/devicemodel/libxendevicemodel.map +1 -0
index 45c773e..130222c 100644
@@ -5,6 +5,7 @@ VERS_1.0 {
 		xendevicemodel_get_ioreq_server_info;
 		xendevicemodel_map_io_range_to_ioreq_server;
 		xendevicemodel_unmap_io_range_from_ioreq_server;
+		xendevicemodel_map_mem_type_to_ioreq_server;
 		xendevicemodel_map_pcidev_to_ioreq_server;
 		xendevicemodel_unmap_pcidev_from_ioreq_server;
 		xendevicemodel_destroy_ioreq_server;
tools/libxc/include/xenctrl_compat.h +5 -0
index 040e7b2..173c08c 100644
@@ -134,6 +134,11 @@ int xc_hvm_map_io_range_to_ioreq_server(
 int xc_hvm_unmap_io_range_from_ioreq_server(
     xc_interface *xch, domid_t domid, ioservid_t id, int is_mmio,
     uint64_t start, uint64_t end);
+int xc_hvm_map_mem_type_to_ioreq_server(
+    xc_interface * xch, domid_t domid, ioservid_t id,
+    uint16_t type, uint32_t flags);
+int xc_hvm_unmap_mem_type_from_ioreq_server(
+    xc_interface * xch, domid_t domid, ioservid_t id, uint16_t type);
 int xc_hvm_map_pcidev_to_ioreq_server(
     xc_interface *xch, domid_t domid, ioservid_t id, uint16_t segment,
     uint8_t bus, uint8_t device, uint8_t function);
tools/libxc/xc_devicemodel_compat.c +17 -0
index e4edeea..ea2496b 100644
@@ -41,6 +41,23 @@ int xc_hvm_unmap_io_range_from_ioreq_server(
                                                            start, end);
 }
 
+int xc_hvm_map_mem_type_to_ioreq_server(
+    xc_interface *xch, domid_t domid, ioservid_t id, uint16_t type,
+    uint32_t flags)
+{
+    return xendevicemodel_map_mem_type_to_ioreq_server(xch->dmod, domid,
+                                                           id, type,
+                                                           flags);
+}
+
+int xc_hvm_unmap_mem_type_from_ioreq_server(
+    xc_interface *xch, domid_t domid, ioservid_t id, uint16_t type)
+{
+    return xendevicemodel_map_mem_type_to_ioreq_server(xch->dmod, domid,
+                                                           id, type,
+                                                           0);
+}
+
 int xc_hvm_map_pcidev_to_ioreq_server(
     xc_interface *xch, domid_t domid, ioservid_t id, uint16_t segment,
     uint8_t bus, uint8_t device, uint8_t function)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#20 lore raw

PATCH v10 4/6 x86/ioreq server: Handle read-modify-write cases for p2m_ioreq_server pages.

In reply to Yu Zhang

In ept_handle_violation(), write violations are also treated as read violations. And when a VM is accessing a write-protected address with read-modify-write instructions, the read emulation process is triggered first.

For p2m_ioreq_server pages, current ioreq server only forwards the write operations to the device model. Therefore when such page is being accessed by a read-modify-write instruction, the read operations should be emulated here in hypervisor. This patch provides such a handler to copy the data to the buffer.

Note: MMIOs with p2m_mmio_dm type do not need such special treatment because both reads and writes will go to the device mode.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

changes in v4:
- Added "Reviewed-by: Jan Beulich <[email protected]>" with one comment change in hvmemul_do_io().

changes in v3:
- According to comments from Jan: clarify comments in hvmemul_do_io().

changes in v2:
- According to comments from Jan: rename mem_ops to ioreq_server_ops.
- According to comments from Jan: use hvm_copy_from_guest_phys() in ioreq_server_read(), instead of do it by myself.

---
 xen/arch/x86/hvm/emulate.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/x86/hvm/emulate.c +34 -0
index dc6f1f2..4de3936 100644
@@ -95,6 +95,26 @@ static const struct hvm_io_handler null_handler = {
     .ops = &null_ops
 };
 
+static int ioreq_server_read(const struct hvm_io_handler *io_handler,
+                    uint64_t addr,
+                    uint32_t size,
+                    uint64_t *data)
+{
+    if ( hvm_copy_from_guest_phys(data, addr, size) != HVMCOPY_okay )
+        return X86EMUL_UNHANDLEABLE;
+
+    return X86EMUL_OKAY;
+}
+
+static const struct hvm_io_ops ioreq_server_ops = {
+    .read = ioreq_server_read,
+    .write = null_write
+};
+
+static const struct hvm_io_handler ioreq_server_handler = {
+    .ops = &ioreq_server_ops
+};
+
 static int hvmemul_do_io(
     bool_t is_mmio, paddr_t addr, unsigned long *reps, unsigned int size,
     uint8_t dir, bool_t df, bool_t data_is_addr, uintptr_t data)
@@ -195,6 +215,9 @@ static int hvmemul_do_io(
          *   a race with an unmap operation on the ioreq server, so re-try the
          *   instruction.
          *
+         *   - If the accesss is a read, this could be part of a
+         *   read-modify-write instruction, emulate the read first.
+         *
          * Note: Even when an ioreq server is found, its value could become
          * stale later, because it is possible that
          *
@@ -228,6 +251,17 @@ static int hvmemul_do_io(
                     vio->io_req.state = STATE_IOREQ_NONE;
                     break;
                 }
+
+                /*
+                 * This is part of a read-modify-write instruction.
+                 * Emulate the read part so we have the value available.
+                 */
+                if ( dir == IOREQ_READ )
+                {
+                    rc = hvm_process_io_intercept(&ioreq_server_handler, &p);
+                    vio->io_req.state = STATE_IOREQ_NONE;
+                    break;
+                }
             }
         }
 
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#21 lore raw

PATCH v10 5/6 x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

This patch disallows mapping of an ioreq server, when there's still p2m_ioreq_server entry left, in case another mapping occurs right after the current one being unmapped, releases its lock, with p2m table not synced yet.

This patch also disallows live migration, when there's remaining p2m_ioreq_server entry in p2m table. The core reason is our current implementation of p2m_change_entry_type_global() lacks information to resync p2m_ioreq_server entries correctly if global_logdirty is on.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>

changes in v5:
- According to comments from Jan: use unsigned long for entry_count;
- According to comments from Jan: refuse mapping requirement when there's p2m_ioreq_server remained in p2m table.
- Added "Reviewed-by: Paul Durrant <[email protected]>"

changes in v4:
- According to comments from Jan: use ASSERT() instead of 'if' condition in p2m_change_type_one().
- According to comments from Jan: commit message changes to mention the p2m_ioreq_server are all based on 4K sized pages.

changes in v3:
- Move the synchronously resetting logic into patch 5.
- According to comments from Jan: introduce p2m_check_changeable() to clarify the p2m type change code.
- According to comments from George: use locks in the same order to avoid deadlock, call p2m_change_entry_type_global() after unmap of the ioreq server is finished.

changes in v2:
- Move the calculation of ioreq server page entry_cout into p2m_change_type_one() so that we do not need a seperate lock.
Note: entry_count is also calculated in resolve_misconfig()/ do_recalc(), fortunately callers of both routines have p2m lock protected already.
- Simplify logic in hvmop_set_mem_type().
- Introduce routine p2m_finish_type_change() to walk the p2m table and do the p2m reset.

---
 xen/arch/x86/hvm/ioreq.c  |  8 ++++++++
 xen/arch/x86/mm/hap/hap.c |  9 +++++++++
 xen/arch/x86/mm/p2m-ept.c |  8 +++++++-
 xen/arch/x86/mm/p2m-pt.c  | 13 +++++++++++--
 xen/arch/x86/mm/p2m.c     | 29 +++++++++++++++++++++++++++++
 xen/include/asm-x86/p2m.h |  9 ++++++++-
 6 files changed, 72 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/ioreq.c +8 -0
index 5bf3b6d..07a6c26 100644
@@ -955,6 +955,14 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
 
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
+    if ( rc == 0 && flags == 0 )
+    {
+        struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+        if ( read_atomic(&p2m->ioreq.entry_count) )
+            p2m_change_entry_type_global(d, p2m_ioreq_server, p2m_ram_rw);
+    }
+
     return rc;
 }
 
xen/arch/x86/mm/hap/hap.c +9 -0
index a57b385..6ec950a 100644
@@ -187,6 +187,15 @@ out:
  */
 static int hap_enable_log_dirty(struct domain *d, bool_t log_global)
 {
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    /*
+     * Refuse to turn on global log-dirty mode if
+     * there's outstanding p2m_ioreq_server pages.
+     */
+    if ( log_global && read_atomic(&p2m->ioreq.entry_count) )
+        return -EBUSY;
+
     /* turn on PG_log_dirty bit in paging mode */
     paging_lock(d);
     d->arch.paging.mode |= PG_log_dirty;
xen/arch/x86/mm/p2m-ept.c +7 -1
index cc1eb21..62e9ddf 100644
@@ -544,6 +544,12 @@ static int resolve_misconfig(struct p2m_domain *p2m, unsigned long gfn)
                     e.ipat = ipat;
                     if ( e.recalc && p2m_is_changeable(e.sa_p2mt) )
                     {
+                         if ( e.sa_p2mt == p2m_ioreq_server )
+                         {
+                             ASSERT(p2m->ioreq.entry_count > 0);
+                             p2m->ioreq.entry_count--;
+                         }
+
                          e.sa_p2mt = p2m_is_logdirty_range(p2m, gfn + i, gfn + i)
                                      ? p2m_ram_logdirty : p2m_ram_rw;
                          ept_p2m_type_to_flags(p2m, &e, e.sa_p2mt, e.access);
@@ -965,7 +971,7 @@ static mfn_t ept_get_entry(struct p2m_domain *p2m,
     if ( is_epte_valid(ept_entry) )
     {
         if ( (recalc || ept_entry->recalc) &&
-             p2m_is_changeable(ept_entry->sa_p2mt) )
+             p2m_check_changeable(ept_entry->sa_p2mt) )
             *t = p2m_is_logdirty_range(p2m, gfn, gfn) ? p2m_ram_logdirty
                                                       : p2m_ram_rw;
         else
xen/arch/x86/mm/p2m-pt.c +11 -2
index c0055f3..0a70dc5 100644
@@ -436,11 +436,13 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn)
          needs_recalc(l1, *pent) )
     {
         l1_pgentry_t e = *pent;
+        p2m_type_t p2mt_old;
 
         if ( !valid_recalc(l1, e) )
             P2M_DEBUG("bogus recalc leaf at d%d:%lx:%u\n",
                       p2m->domain->domain_id, gfn, level);
-        if ( p2m_is_changeable(p2m_flags_to_type(l1e_get_flags(e))) )
+        p2mt_old = p2m_flags_to_type(l1e_get_flags(e));
+        if ( p2m_is_changeable(p2mt_old) )
         {
             unsigned long mask = ~0UL << (level * PAGETABLE_ORDER);
             p2m_type_t p2mt = p2m_is_logdirty_range(p2m, gfn & mask, gfn | ~mask)
@@ -460,6 +462,13 @@ static int do_recalc(struct p2m_domain *p2m, unsigned long gfn)
                      mfn &= ~((unsigned long)_PAGE_PSE_PAT >> PAGE_SHIFT);
                 flags |= _PAGE_PSE;
             }
+
+            if ( p2mt_old == p2m_ioreq_server )
+            {
+                ASSERT(p2m->ioreq.entry_count > 0);
+                p2m->ioreq.entry_count--;
+            }
+
             e = l1e_from_pfn(mfn, flags);
             p2m_add_iommu_flags(&e, level,
                                 (p2mt == p2m_ram_rw)
@@ -729,7 +738,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
 static inline p2m_type_t recalc_type(bool_t recalc, p2m_type_t t,
                                      struct p2m_domain *p2m, unsigned long gfn)
 {
-    if ( !recalc || !p2m_is_changeable(t) )
+    if ( !recalc || !p2m_check_changeable(t) )
         return t;
     return p2m_is_logdirty_range(p2m, gfn, gfn) ? p2m_ram_logdirty
                                                 : p2m_ram_rw;
xen/arch/x86/mm/p2m.c +29 -0
index b84add0..7a1bdd8 100644
@@ -317,6 +317,15 @@ int p2m_set_ioreq_server(struct domain *d,
         if ( p2m->ioreq.server != NULL )
             goto out;
 
+        /*
+         * It is possible that an ioreq server has just been unmapped,
+         * released the spin lock, with some p2m_ioreq_server entries
+         * in p2m table remained. We shall refuse another ioreq server
+         * mapping request in such case.
+         */
+        if ( read_atomic(&p2m->ioreq.entry_count) )
+            goto out;
+
         p2m->ioreq.server = s;
         p2m->ioreq.flags = flags;
     }
@@ -938,6 +947,26 @@ int p2m_change_type_one(struct domain *d, unsigned long gfn,
                          p2m->default_access)
          : -EBUSY;
 
+    if ( !rc )
+    {
+        switch ( nt )
+        {
+        case p2m_ram_rw:
+            if ( ot == p2m_ioreq_server )
+            {
+                ASSERT(p2m->ioreq.entry_count > 0);
+                p2m->ioreq.entry_count--;
+            }
+            break;
+        case p2m_ioreq_server:
+            ASSERT(ot == p2m_ram_rw);
+            p2m->ioreq.entry_count++;
+            break;
+        default:
+            break;
+        }
+    }
+
     gfn_unlock(p2m, gfn, 0);
 
     return rc;
xen/include/asm-x86/p2m.h +8 -1
index 4521620..e7e390d 100644
@@ -120,7 +120,10 @@ typedef unsigned int p2m_query_t;
 
 /* Types that can be subject to bulk transitions. */
 #define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) \
-                              | p2m_to_mask(p2m_ram_logdirty) )
+                              | p2m_to_mask(p2m_ram_logdirty) \
+                              | p2m_to_mask(p2m_ioreq_server) )
+
+#define P2M_IOREQ_TYPES (p2m_to_mask(p2m_ioreq_server))
 
 #define P2M_POD_TYPES (p2m_to_mask(p2m_populate_on_demand))
 
@@ -157,6 +160,7 @@ typedef unsigned int p2m_query_t;
 #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)
 #define p2m_is_discard_write(_t) (p2m_to_mask(_t) & P2M_DISCARD_WRITE_TYPES)
 #define p2m_is_changeable(_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES)
+#define p2m_is_ioreq(_t) (p2m_to_mask(_t) & P2M_IOREQ_TYPES)
 #define p2m_is_pod(_t) (p2m_to_mask(_t) & P2M_POD_TYPES)
 #define p2m_is_grant(_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES)
 /* Grant types are *not* considered valid, because they can be
@@ -178,6 +182,8 @@ typedef unsigned int p2m_query_t;
 
 #define p2m_allows_invalid_mfn(t) (p2m_to_mask(t) & P2M_INVALID_MFN_TYPES)
 
+#define p2m_check_changeable(t) (p2m_is_changeable(t) && !p2m_is_ioreq(t))
+
 typedef enum {
     p2m_host,
     p2m_nested,
@@ -349,6 +355,7 @@ struct p2m_domain {
           * are to be emulated by an ioreq server.
           */
          unsigned int flags;
+         unsigned long entry_count;
      } ioreq;
 };
 
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#40 lore raw

PATCH v10 6/6 x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>

changes in v3:
- According to comments from Paul: use mar_nr, instead of last_gfn for p2m_finish_type_change().
- According to comments from Jan: use gfn_t as type of first_gfn in p2m_finish_type_change().
- According to comments from Jan: simplify the if condition before using p2m_finish_type_change().

changes in v2:
- According to comments from Jan and Andrew: do not use the HVMOP type hypercall continuation method. Instead, adding an opaque in xen_dm_op_map_mem_type_to_ioreq_server to store the gfn.
- According to comments from Jan: change routine's comments and name of parameters of p2m_finish_type_change().

changes in v1:
- This patch is splitted from patch 4 of last version.
- According to comments from Jan: update the gfn_start for when use hypercall continuation to reset the p2m type.
- According to comments from Jan: use min() to compare gfn_end and max mapped pfn in p2m_finish_type_change()

---
 xen/arch/x86/hvm/dm.c     | 41 ++++++++++++++++++++++++++++++++++++++---
 xen/arch/x86/mm/p2m.c     | 29 +++++++++++++++++++++++++++++
 xen/include/asm-x86/p2m.h |  6 ++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/dm.c +38 -3
index 7e0da81..d72b7bd 100644
@@ -384,15 +384,50 @@ static int dm_op(domid_t domid,
 
     case XEN_DMOP_map_mem_type_to_ioreq_server:
     {
-        const struct xen_dm_op_map_mem_type_to_ioreq_server *data =
+        struct xen_dm_op_map_mem_type_to_ioreq_server *data =
             &op.u.map_mem_type_to_ioreq_server;
+        unsigned long first_gfn = data->opaque;
+
+        const_op = false;
 
         rc = -EOPNOTSUPP;
         if ( !hap_enabled(d) )
             break;
 
-        rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
-                                              data->type, data->flags);
+        if ( first_gfn == 0 )
+            rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
+                                                  data->type, data->flags);
+        else
+            rc = 0;
+
+        /*
+         * Iterate p2m table when an ioreq server unmaps from p2m_ioreq_server,
+         * and reset the remaining p2m_ioreq_server entries back to p2m_ram_rw.
+         */
+        if ( rc == 0 && data->flags == 0 )
+        {
+            struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+            while ( read_atomic(&p2m->ioreq.entry_count) &&
+                    first_gfn <= p2m->max_mapped_pfn )
+            {
+                /* Iterate p2m table for 256 gfns each time. */
+                p2m_finish_type_change(d, _gfn(first_gfn), 256,
+                                       p2m_ioreq_server, p2m_ram_rw);
+
+                first_gfn += 256;
+
+                /* Check for continuation if it's not the last iteration. */
+                if ( first_gfn <= p2m->max_mapped_pfn &&
+                     hypercall_preempt_check() )
+                {
+                    rc = -ERESTART;
+                    data->opaque = first_gfn;
+                    break;
+                }
+            }
+        }
+
         break;
     }
 
xen/arch/x86/mm/p2m.c +29 -0
index 7a1bdd8..0daaa86 100644
@@ -1031,6 +1031,35 @@ void p2m_change_type_range(struct domain *d,
     p2m_unlock(p2m);
 }
 
+/* Synchronously modify the p2m type for a range of gfns from ot to nt. */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn, unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    p2m_type_t t;
+    unsigned long gfn = gfn_x(first_gfn);
+    unsigned long last_gfn = gfn + max_nr - 1;
+
+    ASSERT(ot != nt);
+    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
+
+    p2m_lock(p2m);
+
+    last_gfn = min(last_gfn, p2m->max_mapped_pfn);
+    while ( gfn <= last_gfn )
+    {
+        get_gfn_query_unlocked(d, gfn, &t);
+
+        if ( t == ot )
+            p2m_change_type_one(d, gfn, t, nt);
+
+        gfn++;
+    }
+
+    p2m_unlock(p2m);
+}
+
 /*
  * Returns:
  *    0              for success
xen/include/asm-x86/p2m.h +6 -0
index e7e390d..0e670af 100644
@@ -611,6 +611,12 @@ void p2m_change_type_range(struct domain *d,
 int p2m_change_type_one(struct domain *d, unsigned long gfn,
                         p2m_type_t ot, p2m_type_t nt);
 
+/* Synchronously change the p2m type for a range of gfns */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn,
+                            unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt);
+
 /* Report a change affecting memory types. */
 void p2m_memory_type_changed(struct domain *d);
 
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v10 3/6
#6 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 4 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

Quoted 8 lines
---
 tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
 tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
 tools/libs/devicemodel/libxendevicemodel.map    |  1 +
 tools/libxc/include/xenctrl_compat.h            |  5 +++++
 tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++

This is new code so I don't think you really want compat code for this, do you?

Quoted 132 lines
 5 files changed, 66 insertions(+)
tools/libs/devicemodel/core.c +0 -0
index a85cb49..ff09819 100644
@@ -244,6 +244,31 @@ int

xendevicemodel_unmap_io_range_from_ioreq_server(

     return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));

}

tools/libs/devicemodel/core.c +2 -0
+int xendevicemodel_map_mem_type_to_ioreq_server(
+    xendevicemodel_handle *dmod, domid_t domid, ioservid_t id, uint16_t

type,

tools/libs/devicemodel/core.c +23 -0
+    uint32_t flags)
+{
+    struct xen_dm_op op;
+    struct xen_dm_op_map_mem_type_to_ioreq_server *data;
+
+    if (type != HVMMEM_ioreq_server ||
+        flags & ~XEN_DMOP_IOREQ_MEM_ACCESS_WRITE) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    memset(&op, 0, sizeof(op));
+
+    op.op = XEN_DMOP_map_mem_type_to_ioreq_server;
+    data = &op.u.map_mem_type_to_ioreq_server;
+
+    data->id = id;
+    data->type = type;
+    data->flags = flags;
+
+    return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));
+}
+
 int xendevicemodel_map_pcidev_to_ioreq_server(
     xendevicemodel_handle *dmod, domid_t domid, ioservid_t id,
     uint16_t segment, uint8_t bus, uint8_t device, uint8_t function)
tools/libs/devicemodel/include/xendevicemodel.h +0 -0
index b3f600e..1da216f 100644
@@ -104,6 +104,24 @@ int

xendevicemodel_unmap_io_range_from_ioreq_server(

     uint64_t start, uint64_t end);

/**
+ * This function registers/deregisters a memory type for emulation.
+ *
+ * @parm dmod a handle to an open devicemodel interface.
+ * @parm domid the domain id to be serviced.
+ * @parm id the IOREQ Server id.
+ * @parm type the memory type to be emulated. For now, only HVMMEM_ioreq_server
+ * is supported, and in the future new types can be introduced, e.g.
+ * HVMMEM_ioreq_serverX mapped to ioreq server X.
+ * @parm flags operations to be emulated; 0 for unmap. For now, only write
+ * operations will be emulated and can be extended to emulate
+ * read ones in the future.
+ * @return 0 on success, -1 on failure.
+ */

tools/libs/devicemodel/include/xendevicemodel.h +2 -0
+int xendevicemodel_map_mem_type_to_ioreq_server(
+    xendevicemodel_handle *dmod, domid_t domid, ioservid_t id, uint16_t

type,

tools/libs/devicemodel/include/xendevicemodel.h +3 -0
+    uint32_t flags);
+
+/**
  * This function registers a PCI device for config space emulation.
  *
  * @parm dmod a handle to an open devicemodel interface.
tools/libs/devicemodel/libxendevicemodel.map +1 -0
index 45c773e..130222c 100644
@@ -5,6 +5,7 @@ VERS_1.0 {
 		xendevicemodel_get_ioreq_server_info;
 		xendevicemodel_map_io_range_to_ioreq_server;
 		xendevicemodel_unmap_io_range_from_ioreq_server;
+		xendevicemodel_map_mem_type_to_ioreq_server;
 		xendevicemodel_map_pcidev_to_ioreq_server;
 		xendevicemodel_unmap_pcidev_from_ioreq_server;
 		xendevicemodel_destroy_ioreq_server;
tools/libxc/include/xenctrl_compat.h +5 -0
index 040e7b2..173c08c 100644
@@ -134,6 +134,11 @@ int xc_hvm_map_io_range_to_ioreq_server(
 int xc_hvm_unmap_io_range_from_ioreq_server(
     xc_interface *xch, domid_t domid, ioservid_t id, int is_mmio,
     uint64_t start, uint64_t end);
+int xc_hvm_map_mem_type_to_ioreq_server(
+    xc_interface * xch, domid_t domid, ioservid_t id,
+    uint16_t type, uint32_t flags);
+int xc_hvm_unmap_mem_type_from_ioreq_server(
+    xc_interface * xch, domid_t domid, ioservid_t id, uint16_t type);
 int xc_hvm_map_pcidev_to_ioreq_server(
     xc_interface *xch, domid_t domid, ioservid_t id, uint16_t segment,
     uint8_t bus, uint8_t device, uint8_t function);
tools/libxc/xc_devicemodel_compat.c +17 -0
index e4edeea..ea2496b 100644
@@ -41,6 +41,23 @@ int xc_hvm_unmap_io_range_from_ioreq_server(
                                                            start, end);
 }
 
+int xc_hvm_map_mem_type_to_ioreq_server(
+    xc_interface *xch, domid_t domid, ioservid_t id, uint16_t type,
+    uint32_t flags)
+{
+    return xendevicemodel_map_mem_type_to_ioreq_server(xch->dmod,
 domid,
+                                                           id, type,
+                                                           flags);
+}
+
+int xc_hvm_unmap_mem_type_from_ioreq_server(
+    xc_interface *xch, domid_t domid, ioservid_t id, uint16_t type)
+{
+    return xendevicemodel_map_mem_type_to_ioreq_server(xch->dmod,
 domid,
+                                                           id, type,
+                                                           0);
+}
+
 int xc_hvm_map_pcidev_to_ioreq_server(
Quoted 4 lines

xc_interface *xch, domid_t domid, ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device, uint8_t function)
--
1.9.1

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

Reviewed-by on v10 6/6
#41 lore raw

Re: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Yu Zhang
Quoted 15 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>
Subject: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

Quoted 6 lines

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

Quoted 153 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>

changes in v3:
- According to comments from Paul: use mar_nr, instead of last_gfn for p2m_finish_type_change().
- According to comments from Jan: use gfn_t as type of first_gfn in p2m_finish_type_change().
- According to comments from Jan: simplify the if condition before using p2m_finish_type_change().

changes in v2:
- According to comments from Jan and Andrew: do not use the HVMOP type hypercall continuation method. Instead, adding an opaque in xen_dm_op_map_mem_type_to_ioreq_server to store the gfn.
- According to comments from Jan: change routine's comments and name of parameters of p2m_finish_type_change().

changes in v1:
- This patch is splitted from patch 4 of last version.
- According to comments from Jan: update the gfn_start for when use hypercall continuation to reset the p2m type.
- According to comments from Jan: use min() to compare gfn_end and max mapped pfn in p2m_finish_type_change()

---
 xen/arch/x86/hvm/dm.c     | 41
++++++++++++++++++++++++++++++++++++++---
 xen/arch/x86/mm/p2m.c     | 29 +++++++++++++++++++++++++++++
 xen/include/asm-x86/p2m.h |  6 ++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/dm.c +13 -3
index 7e0da81..d72b7bd 100644
@@ -384,15 +384,50 @@ static int dm_op(domid_t domid,
 
     case XEN_DMOP_map_mem_type_to_ioreq_server:
     {
-        const struct xen_dm_op_map_mem_type_to_ioreq_server *data =
+        struct xen_dm_op_map_mem_type_to_ioreq_server *data =
             &op.u.map_mem_type_to_ioreq_server;
+        unsigned long first_gfn = data->opaque;
+
+        const_op = false;
 
         rc = -EOPNOTSUPP;
         if ( !hap_enabled(d) )
             break;
 
-        rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
-                                              data->type, data->flags);
+        if ( first_gfn == 0 )
+            rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
+                                                  data->type, data->flags);
+        else
+            rc = 0;
+
+        /*
+         * Iterate p2m table when an ioreq server unmaps from
 p2m_ioreq_server,
+         * and reset the remaining p2m_ioreq_server entries back to

p2m_ram_rw.

xen/arch/x86/hvm/dm.c +25 -0
+         */
+        if ( rc == 0 && data->flags == 0 )
+        {
+            struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+            while ( read_atomic(&p2m->ioreq.entry_count) &&
+                    first_gfn <= p2m->max_mapped_pfn )
+            {
+                /* Iterate p2m table for 256 gfns each time. */
+                p2m_finish_type_change(d, _gfn(first_gfn), 256,
+                                       p2m_ioreq_server, p2m_ram_rw);
+
+                first_gfn += 256;
+
+                /* Check for continuation if it's not the last iteration. */
+                if ( first_gfn <= p2m->max_mapped_pfn &&
+                     hypercall_preempt_check() )
+                {
+                    rc = -ERESTART;
+                    data->opaque = first_gfn;
+                    break;
+                }
+            }
+        }
+
         break;
     }
xen/arch/x86/mm/p2m.c +29 -0
index 7a1bdd8..0daaa86 100644
@@ -1031,6 +1031,35 @@ void p2m_change_type_range(struct domain *d,
     p2m_unlock(p2m);
 }
 
+/* Synchronously modify the p2m type for a range of gfns from ot to nt. */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn, unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    p2m_type_t t;
+    unsigned long gfn = gfn_x(first_gfn);
+    unsigned long last_gfn = gfn + max_nr - 1;
+
+    ASSERT(ot != nt);
+    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
+
+    p2m_lock(p2m);
+
+    last_gfn = min(last_gfn, p2m->max_mapped_pfn);
+    while ( gfn <= last_gfn )
+    {
+        get_gfn_query_unlocked(d, gfn, &t);
+
+        if ( t == ot )
+            p2m_change_type_one(d, gfn, t, nt);
+
+        gfn++;
+    }
+
+    p2m_unlock(p2m);
+}
+
 /*
  * Returns:
  *    0              for success
xen/include/asm-x86/p2m.h +6 -1
index e7e390d..0e670af 100644
@@ -611,6 +611,12 @@ void p2m_change_type_range(struct domain *d,
 int p2m_change_type_one(struct domain *d, unsigned long gfn,
                         p2m_type_t ot, p2m_type_t nt);
 
+/* Synchronously change the p2m type for a range of gfns */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn,
+                            unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt);
+
 /* Report a change affecting memory types. */
 void p2m_memory_type_changed(struct domain *d);
 
--
Quoted 1 line

1.9.1

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

#7 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Paul Durrant
Quoted 43 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 4 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

Quoted 8 lines
---
 tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
 tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
 tools/libs/devicemodel/libxendevicemodel.map    |  1 +
 tools/libxc/include/xenctrl_compat.h            |  5 +++++
 tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Wei.

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

Reviewed-by on v10 2/6
#3 lore raw

Re: [PATCH v10 2/6] x86/ioreq server: Add DMOP to map guest ram with p2m_ioreq_server to an ioreq server.

In reply to Yu Zhang
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:

Previously, p2m_ioreq_server is used to write-protect guest ram pages, which are tracked with ioreq server's rangeset. However, number of ram pages to be tracked may exceed the upper limit of rangeset.

Now, a new DMOP - XEN_DMOP_map_mem_type_to_ioreq_server, is added to let one ioreq server claim/disclaim its responsibility for the handling of guest pages with p2m type p2m_ioreq_server. Users of this DMOP can specify which kind of operation is supposed to be emulated in a parameter named flags. Currently, this DMOP only support the emulation of write operations. And it can be further extended to support the emulation of read ones if an ioreq server has such requirement in the future.

For now, we only support one ioreq server for this p2m type, so once an ioreq server has claimed its ownership, subsequent calls of the XEN_DMOP_map_mem_type_to_ioreq_server will fail. Users can also disclaim the ownership of guest ram pages with p2m_ioreq_server, by triggering this new DMOP, with ioreq server id set to the current owner's and flags parameter set to 0.

Note:
a> both XEN_DMOP_map_mem_type_to_ioreq_server and p2m_ioreq_server are only supported for HVMs with HAP enabled.

Quoted 5 lines

b> only after one ioreq server claims its ownership of p2m_ioreq_server, will the p2m type change to p2m_ioreq_server be allowed.

c> this patch shall be accepted together with the following ones in this series.

Fur the future: This last note doesn't really belong here, but after the first --- separator, as there's no value having this in git.

Quoted 3 lines

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

#22 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +0 -0
@@ -317,6 +317,15 @@ int p2m_set_ioreq_server(struct domain *d,
         if ( p2m->ioreq.server != NULL )

xen/arch/x86/mm/p2m.c int p2m_set_ioreq_server(struct domain *d, · line 327

Quoted 10 lines
xen/arch/x86/mm/p2m.c +8 -0
             goto out;
 
+        /*
+         * It is possible that an ioreq server has just been unmapped,
+         * released the spin lock, with some p2m_ioreq_server entries
+         * in p2m table remained. We shall refuse another ioreq server
+         * mapping request in such case.
+         */
+        if ( read_atomic(&p2m->ioreq.entry_count) )
+            goto out;

So this produces the same -EINVAL as the earlier check in context above. I think it would be nice if neither did - -EINUSE for the first (which we don't have, so -EOPNOTSUPP would seem the second bets option there) and -EBUSY for the second would seem more appropriate. If you agree, respective adjustments could be done while committing, if no other reason for a v11 arises.

Jan

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

Reviewed-by
#23 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines · On 03.04.17 at 16:36, <[email protected]> wrote:
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +0 -0
@@ -317,6 +317,15 @@ int p2m_set_ioreq_server(struct domain *d,
         if ( p2m->ioreq.server != NULL )
             goto out;
 
Quoted 15 lines

xen/arch/x86/mm/p2m.c int p2m_set_ioreq_server(struct domain *d, · line 327

Quoted 8 lines
xen/arch/x86/mm/p2m.c +8 -0
+        /*
+         * It is possible that an ioreq server has just been unmapped,
+         * released the spin lock, with some p2m_ioreq_server entries
+         * in p2m table remained. We shall refuse another ioreq server
+         * mapping request in such case.
+         */
+        if ( read_atomic(&p2m->ioreq.entry_count) )
+            goto out;

So this produces the same -EINVAL as the earlier check in context above. I think it would be nice if neither did - -EINUSE for the first (which we don't have, so -EOPNOTSUPP would seem the second bets option there) and -EBUSY for the second would seem more appropriate. If you agree, respective adjustments could be done while committing, if no other reason for a v11 arises.

Oh, and with that

Jan

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

Reviewed-by on v10 6/6
#42 lore raw

Re: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Yu Zhang
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

albeit I think ...

Quoted 24 lines
xen/arch/x86/mm/p2m.c +18 -0
@@ -1031,6 +1031,35 @@ void p2m_change_type_range(struct domain *d,
     p2m_unlock(p2m);
 }
 
+/* Synchronously modify the p2m type for a range of gfns from ot to nt. */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn, unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    p2m_type_t t;
+    unsigned long gfn = gfn_x(first_gfn);
+    unsigned long last_gfn = gfn + max_nr - 1;
+
+    ASSERT(ot != nt);
+    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
+
+    p2m_lock(p2m);
+
+    last_gfn = min(last_gfn, p2m->max_mapped_pfn);
+    while ( gfn <= last_gfn )
+    {

xen/arch/x86/mm/p2m.c void p2m_change_type_range(struct domain *d, · line 1061

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+        get_gfn_query_unlocked(d, gfn, &t);
+
+        if ( t == ot )
+            p2m_change_type_one(d, gfn, t, nt);
+
+        gfn++;
+    }
+
+    p2m_unlock(p2m);
+}

... this could be improved for reduction of overall latency: A fixed limit as input if not very useful, as it does matter quite a bit whether need to call p2m_change_type_one(). Doing 256 iteration with no single call is going to be pretty fast I think, so it would be desirable to weigh differently iterations with and without call to that function.

Jan

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

author
#8 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Wei Liu
Quoted 42 lines · Wei Liu
Quoted 41 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 12 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
  tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
  tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
  tools/libs/devicemodel/libxendevicemodel.map    |  1 +
  tools/libxc/include/xenctrl_compat.h            |  5 +++++
  tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 2 lines
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this.
What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code not changed, maybe we shall not add the new op to libxendevicemodel.map either.

I can send a V11 of this series - but must be quick to catch up the code freeze date. :) Or with other patches received "Reviewed by", we can just drop the useless code of this patch. Any suggestions?

B.R.
Yu

Quoted 2 lines

Wei.

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

author
#24 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

So this produces the same -EINVAL as the earlier check in context above. I think it would be nice if neither did - -EINUSE for the first (which we don't have, so -EOPNOTSUPP would seem the second bets option there) and -EBUSY for the second would seem more appropriate. If you agree, respective adjustments could be done while committing, if no other reason for a v11 arises.

Thanks Jan.
But my code shows both will return -EBUSY, instead of -EINVAL for the mapping requirement:

      /* Unmap ioreq server from p2m type by passing flags with 0. */
      if ( flags == 0 )
      {
/rc = -EINVAL;/
          if ( p2m->ioreq.server != s )
              goto out;

          p2m->ioreq.server = NULL;
          p2m->ioreq.flags = 0;
      }
      else
      {
/rc = -EBUSY;/
          if ( p2m->ioreq.server != NULL )
              goto out;

          /*
           * It is possible that an ioreq server has just been unmapped,
           * released the spin lock, with some p2m_ioreq_server entries
           * in p2m table remained. We shall refuse another ioreq server
           * mapping request in such case.
           */
          if ( read_atomic(&p2m->ioreq.entry_count) )
              goto out;

          p2m->ioreq.server = s;
          p2m->ioreq.flags = flags;
      }

Are you really wanna use -EOPNOTSUPP when p2m->ioreq.server is not NULL for the mapping code?

For the unmapping code, -EINVAL is used when the ioreq server to be unmapped is not the designated one. But for this one, I am not sure which one is better: -EINVAL or -EBUSY?

Yu

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

#25 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 09:18, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

So this produces the same -EINVAL as the earlier check in context above. I think it would be nice if neither did - -EINUSE for the first (which we don't have, so -EOPNOTSUPP would seem the second bets option there) and -EBUSY for the second would seem more appropriate. If you agree, respective adjustments could be done while committing, if no other reason for a v11 arises.

Thanks Jan.

First of all - deleting irrelevant context from replies is always appreciated, but I think you went too far here. It is no longer possible to re-associate your comments with the change you've made (not visible from the code fragment below).

Quoted 18 lines

But my code shows both will return -EBUSY, instead of -EINVAL for the mapping requirement:

       /* Unmap ioreq server from p2m type by passing flags with 0. */
       if ( flags == 0 )
       {
/rc = -EINVAL;/
           if ( p2m->ioreq.server != s )
               goto out;

           p2m->ioreq.server = NULL;
           p2m->ioreq.flags = 0;
       }
       else
       {
/rc = -EBUSY;/
           if ( p2m->ioreq.server != NULL )
               goto out;
Quoted 8 lines
           /*
            * It is possible that an ioreq server has just been unmapped,
            * released the spin lock, with some p2m_ioreq_server entries
            * in p2m table remained. We shall refuse another ioreq server
            * mapping request in such case.
            */
           if ( read_atomic(&p2m->ioreq.entry_count) )
               goto out;

Oh, I see: I've mistakenly assumed to addition was to the if() branch. I clearly didn't look closely enough, I'm sorry.

Quoted 6 lines
           p2m->ioreq.server = s;
           p2m->ioreq.flags = flags;
       }

Are you really wanna use -EOPNOTSUPP when p2m->ioreq.server is not NULL for the mapping code?

As per above, I really did refer to the wrong piece of code, so all is fine the way the code is.

Jan

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

author
#43 lore raw

Re: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Jan Beulich
Quoted 45 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

albeit I think ...

Quoted 31 lines
xen/arch/x86/mm/p2m.c +25 -0
@@ -1031,6 +1031,35 @@ void p2m_change_type_range(struct domain *d,
      p2m_unlock(p2m);
  }
 
+/* Synchronously modify the p2m type for a range of gfns from ot to nt. */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn, unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    p2m_type_t t;
+    unsigned long gfn = gfn_x(first_gfn);
+    unsigned long last_gfn = gfn + max_nr - 1;
+
+    ASSERT(ot != nt);
+    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
+
+    p2m_lock(p2m);
+
+    last_gfn = min(last_gfn, p2m->max_mapped_pfn);
+    while ( gfn <= last_gfn )
+    {
+        get_gfn_query_unlocked(d, gfn, &t);
+
+        if ( t == ot )
+            p2m_change_type_one(d, gfn, t, nt);
+
+        gfn++;
+    }
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_change_type_range(struct domain *d, · line 1061

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+
+    p2m_unlock(p2m);
+}

... this could be improved for reduction of overall latency: A fixed limit as input if not very useful, as it does matter quite a bit whether need to call p2m_change_type_one(). Doing 256 iteration with no single call is going to be pretty fast I think, so it would be desirable to weigh differently iterations with and without call to that function.

Oh, thanks for your advice, very enlightening.
With code freeze date approaching, how about we solve this in a separate patch? :) I've drafted one, and will send it out in another mail. Hope the direction is correct.

B.R.
Yu

Quoted 7 lines

Jan

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

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

#9 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 08:53, <[email protected]> wrote:

Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Without the libxc wrapper, the new DMOP is effectively dead code too. All or nothing, imo.

Jan

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

author
#10 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

Quoted 6 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 08:53, <[email protected]> wrote:

Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Without the libxc wrapper, the new DMOP is effectively dead code too. All or nothing, imo.

Thanks Jan. But I am confused. :(

Previously I thought the libxc needs to be updated together with libxendevicemodel. But from Paul & Wei's reply, and previous commit from Paul, my new understanding is that these libxc interfaces are only compatible code for old ones, and libxendevicemodel can be triggered, without through libxc interfaces.

Is my new interpretation wrong?

Thanks
Yu

Quoted 3 lines

Jan

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

#11 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 11:22, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 08:53, <[email protected]> wrote:

Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Without the libxc wrapper, the new DMOP is effectively dead code too. All or nothing, imo.

Thanks Jan. But I am confused. :(

Previously I thought the libxc needs to be updated together with libxendevicemodel.
But from Paul & Wei's reply, and previous commit from Paul, my new understanding
is that these libxc interfaces are only compatible code for old ones, and libxendevicemodel
can be triggered, without through libxc interfaces.

Quoted 1 line

Is my new interpretation wrong?

Oops, I'm sorry, I still have to get used to the split. I indeed meant "without the libxendevicemodel wrapper ..."

Jan

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

#44 lore raw

Re: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Yu Zhang
Quoted 58 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 11:11, <[email protected]> wrote:
Quoted 45 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 02.04.17 at 14:24, <[email protected]> wrote:

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

albeit I think ...

Quoted 31 lines
xen/arch/x86/mm/p2m.c +25 -0
@@ -1031,6 +1031,35 @@ void p2m_change_type_range(struct domain *d,
      p2m_unlock(p2m);
  }
 
+/* Synchronously modify the p2m type for a range of gfns from ot to nt. */
+void p2m_finish_type_change(struct domain *d,
+                            gfn_t first_gfn, unsigned long max_nr,
+                            p2m_type_t ot, p2m_type_t nt)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    p2m_type_t t;
+    unsigned long gfn = gfn_x(first_gfn);
+    unsigned long last_gfn = gfn + max_nr - 1;
+
+    ASSERT(ot != nt);
+    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
+
+    p2m_lock(p2m);
+
+    last_gfn = min(last_gfn, p2m->max_mapped_pfn);
+    while ( gfn <= last_gfn )
+    {
+        get_gfn_query_unlocked(d, gfn, &t);
+
+        if ( t == ot )
+            p2m_change_type_one(d, gfn, t, nt);
+
+        gfn++;
+    }
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_change_type_range(struct domain *d, · line 1061

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+
+    p2m_unlock(p2m);
+}

... this could be improved for reduction of overall latency: A fixed limit as input if not very useful, as it does matter quite a bit whether need to call p2m_change_type_one(). Doing 256 iteration with no single call is going to be pretty fast I think, so it would be desirable to weigh differently iterations with and without call to that function.

Quoted 3 lines

Oh, thanks for your advice, very enlightening.
With code freeze date approaching, how about we solve this in a separate patch? :)

Well, that's what I've tried to express by giving my R-b with the rest simply being a remark.

Jan

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

#12 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang
Quoted 46 lines · Yu Zhang
Quoted 43 lines · Wei Liu
Quoted 42 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 4 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

Quoted 8 lines
---
  tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
  tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
  tools/libs/devicemodel/libxendevicemodel.map    |  1 +
  tools/libxc/include/xenctrl_compat.h            |  5 +++++
  tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++

This is new code so I don't think you really want compat code for this, do you?

Quoted 9 lines
Quoted 1 line

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

The modification to libxendevicemodel.map needs to stay.

Quoted 6 lines

I can send a V11 of this series - but must be quick to catch up the code freeze date. :)
Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Please drop the compat wrapper.

Wei.

Quoted 7 lines

B.R.
Yu

Quoted 2 lines

Wei.

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

#13 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

Quoted 57 lines · Wei Liu
Quoted 46 lines · Yu Zhang
Quoted 43 lines · Wei Liu
Quoted 42 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 4 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

Quoted 8 lines
---
  tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
  tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
  tools/libs/devicemodel/libxendevicemodel.map    |  1 +
  tools/libxc/include/xenctrl_compat.h            |  5 +++++
  tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++

This is new code so I don't think you really want compat code for this, do you?

Quoted 9 lines
Quoted 1 line

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

Quoted 2 lines

The modification to libxendevicemodel.map needs to stay.

And, if you're interested in the rationale behind this.

https://software.intel.com/sites/default/files/m/a/1/e/dsohowto.pdf

Wei.

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

author
#15 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Wei Liu
Quoted 56 lines · Wei Liu
Quoted 44 lines · Yu Zhang
Quoted 42 lines · Wei Liu
Quoted 41 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 12 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
   tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
   tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
   tools/libs/devicemodel/libxendevicemodel.map    |  1 +
   tools/libxc/include/xenctrl_compat.h            |  5 +++++
   tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 9 lines
Quoted 2 lines
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

The modification to libxendevicemodel.map needs to stay.

Quoted 7 lines
Quoted 6 lines

I can send a V11 of this series - but must be quick to catch up the code freeze date. :)
Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Please drop the compat wrapper.

Thank you, Wei.
So this series is OK for merge. And with compat wrapper dropped while committing, we do not need send the V11, right?

B.R.
Yu

Quoted 7 lines

Wei.

Quoted 5 lines

B.R.
Yu

Quoted 2 lines

Wei.

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

author
#14 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Wei Liu
Quoted 55 lines · Wei Liu
Quoted 54 lines · Wei Liu
Quoted 53 lines · Yu Zhang
Quoted 42 lines · Wei Liu
Quoted 41 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 12 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
   tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
   tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
   tools/libs/devicemodel/libxendevicemodel.map    |  1 +
   tools/libxc/include/xenctrl_compat.h            |  5 +++++
   tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 2 lines
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

Quoted 5 lines
Quoted 2 lines

The modification to libxendevicemodel.map needs to stay.

And, if you're interested in the rationale behind this.

https://software.intel.com/sites/default/files/m/a/1/e/dsohowto.pdf

Wah. Great!
Thanks for your information! :)

Yu

Quoted 2 lines

Wei.

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

author
#17 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Wei Liu
Quoted 64 lines · Wei Liu
Quoted 63 lines · Yu Zhang
Quoted 61 lines · Wei Liu
Quoted 43 lines · Yu Zhang
Quoted 42 lines · Wei Liu
Quoted 41 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 12 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
    tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
    tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
    tools/libs/devicemodel/libxendevicemodel.map    |  1 +
    tools/libxc/include/xenctrl_compat.h            |  5 +++++
    tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 9 lines
Quoted 2 lines
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

The modification to libxendevicemodel.map needs to stay.

Quoted 6 lines

I can send a V11 of this series - but must be quick to catch up the code freeze date. :)
Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Please drop the compat wrapper.

Thank you, Wei.
So this series is OK for merge. And with compat wrapper dropped while committing,
we do not need send the V11, right?

Assuming Jan is happy with the HV code as-is. Yes, I can drop that hunk and commit this series.

Thanks, Wei.
Jan, can we reach consensus on this? :)

Yu

Quoted 12 lines

Wei.

Quoted 10 lines

B.R.
Yu

Quoted 7 lines

Wei.

Quoted 5 lines

B.R.
Yu

Quoted 2 lines

Wei.

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

#16 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang
Quoted 65 lines · Yu Zhang
Quoted 62 lines · Wei Liu
Quoted 44 lines · Yu Zhang
Quoted 42 lines · Wei Liu
Quoted 41 lines · Paul Durrant
Quoted 18 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 02 April 2017 13:24
To: [email protected]
Cc: [email protected]; Paul Durrant <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

A new device model wrapper is added for the newly introduced DMOP - XEN_DMOP_map_mem_type_to_ioreq_server.

Since currently this DMOP only supports the emulation of write operations, attempts to trigger the DMOP with values other than XEN_DMOP_IOREQ_MEM_ACCESS_WRITE or 0(to unmap the ioreq server) shall fail. The wrapper shall be updated once read operations are also to be emulated in the future.

Quoted 7 lines

Also note currently this DMOP only supports one memory type, and can be extended in the future to map multiple memory types to multiple ioreq servers, e.g. mapping HVMMEM_ioreq_serverX to ioreq server X, This wrapper shall be updated when such change is made.

...with one observation...

Quoted 12 lines

---
Cc: Paul Durrant <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
   tools/libs/devicemodel/core.c                   | 25
+++++++++++++++++++++++++
   tools/libs/devicemodel/include/xendevicemodel.h | 18
++++++++++++++++++
   tools/libs/devicemodel/libxendevicemodel.map    |  1 +
   tools/libxc/include/xenctrl_compat.h            |  5 +++++
   tools/libxc/xc_devicemodel_compat.c             | 17 +++++++++++++++++
Quoted 9 lines
Quoted 2 lines
Quoted 1 line

This is new code so I don't think you really want compat code for this, do you?

Correct. No compat code is needed.

Thank you, Paul & Wei.
Oh. I see, the compat code is only for existing ones. We do not need this. What about the libxendevicemodel.map? I updated this file to build pass for the libxc, with compat code
not changed, maybe we shall not add the new op to libxendevicemodel.map either.

The modification to libxendevicemodel.map needs to stay.

Quoted 6 lines

I can send a V11 of this series - but must be quick to catch up the code freeze date. :)
Or with other patches received "Reviewed by", we can just drop the useless code of this patch.
Any suggestions?

Quoted 7 lines
Quoted 1 line

Please drop the compat wrapper.

Thank you, Wei.
So this series is OK for merge. And with compat wrapper dropped while committing,
we do not need send the V11, right?

Assuming Jan is happy with the HV code as-is. Yes, I can drop that hunk and commit this series.

Wei.

Quoted 11 lines

B.R.
Yu

Quoted 7 lines

Wei.

Quoted 5 lines

B.R.
Yu

Quoted 2 lines

Wei.

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

#18 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

In reply to Yu Zhang
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 12:26, <[email protected]> wrote:
Quoted 5 lines · Wei Liu
Quoted 4 lines · Yu Zhang

So this series is OK for merge. And with compat wrapper dropped while committing,
we do not need send the V11, right?

Quoted 5 lines
Quoted 2 lines

Assuming Jan is happy with the HV code as-is. Yes, I can drop that hunk and commit this series.

Thanks, Wei.
Jan, can we reach consensus on this? :)

As mentioned on irc, there are still some missing acks for some of the patches.

Jan

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

author
#19 lore raw

Re: [PATCH v10 3/6] x86/ioreq server: Add device model wrappers for new DMOP

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 12:26, <[email protected]> wrote:
Quoted 5 lines · Wei Liu
Quoted 4 lines · Yu Zhang

So this series is OK for merge. And with compat wrapper dropped while committing,
we do not need send the V11, right?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Assuming Jan is happy with the HV code as-is. Yes, I can drop that hunk and commit this series.

Thanks, Wei.
Jan, can we reach consensus on this? :)

As mentioned on irc, there are still some missing acks for some of the patches.

Yeah, thanks Jan.
Guess you mean George's. May god bless... :)

Yu

Quoted 7 lines

Jan

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

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

Reviewed-by on v10 2/6
#4 lore raw

Re: [PATCH v10 2/6] x86/ioreq server: Add DMOP to map guest ram with p2m_ioreq_server to an ioreq server.

In reply to Yu Zhang
Quoted 28 lines · Yu Zhang

Previously, p2m_ioreq_server is used to write-protect guest ram pages, which are tracked with ioreq server's rangeset. However, number of ram pages to be tracked may exceed the upper limit of rangeset.

Now, a new DMOP - XEN_DMOP_map_mem_type_to_ioreq_server, is added to let one ioreq server claim/disclaim its responsibility for the handling of guest pages with p2m type p2m_ioreq_server. Users of this DMOP can specify which kind of operation is supposed to be emulated in a parameter named flags. Currently, this DMOP only support the emulation of write operations. And it can be further extended to support the emulation of read ones if an ioreq server has such requirement in the future.

For now, we only support one ioreq server for this p2m type, so once an ioreq server has claimed its ownership, subsequent calls of the XEN_DMOP_map_mem_type_to_ioreq_server will fail. Users can also disclaim the ownership of guest ram pages with p2m_ioreq_server, by triggering this new DMOP, with ioreq server id set to the current owner's and flags parameter set to 0.

Note:
a> both XEN_DMOP_map_mem_type_to_ioreq_server and p2m_ioreq_server are only supported for HVMs with HAP enabled.

b> only after one ioreq server claims its ownership of p2m_ioreq_server, will the p2m type change to p2m_ioreq_server be allowed.

Quoted 6 lines

c> this patch shall be accepted together with the following ones in this series.

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

#26 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Other than that, only one minor comment...

xen/arch/x86/mm/hap/hap.c out: · line 194

Quoted 13 lines
xen/arch/x86/mm/hap/hap.c +5 -0
index a57b385..6ec950a 100644
@@ -187,6 +187,15 @@ out:
  */
 static int hap_enable_log_dirty(struct domain *d, bool_t log_global)
 {
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    /*
+     * Refuse to turn on global log-dirty mode if
+     * there's outstanding p2m_ioreq_server pages.

Grammar nit: if *there are* outstanding pages.

-George

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

Reviewed-by on v10 6/6
#45 lore raw

Re: [PATCH v10 6/6] x86/ioreq server: Synchronously reset outstanding p2m_ioreq_server entries when an ioreq server unmaps.

In reply to Yu Zhang
Quoted 10 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this synchronously by iterating the p2m table.

The synchronous resetting is necessary because we need to guarantee the p2m table is clean before another ioreq server is mapped. And since the sweeping of p2m table could be time consuming, it is done with hypercall continuation.

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

author
#27 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to George Dunlap
Quoted 16 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

Quoted 7 lines

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because it is already allocated.

And even when emulation is finished, the balloon driver successfully get this page, and triggers decrease_reservation, the purpose is to remove the current mapping relation between the gfn and mfn in p2m. So IIUC, p2m_remove_page() will be triggered if everything is goes fine, and then p2m_set_entry(), which will trigger the recalc logic eventually, either in ept_set_entry() or p2m_pt_set_entry(). Then the entry_count will be updated in the recalc logic.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +0 -0
index a57b385..6ec950a 100644
@@ -187,6 +187,15 @@ out:
   */
Quoted 8 lines

xen/arch/x86/mm/hap/hap.c out: · line 194

Quoted 7 lines
xen/arch/x86/mm/hap/hap.c +5 -0
  static int hap_enable_log_dirty(struct domain *d, bool_t log_global)
  {
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    /*
+     * Refuse to turn on global log-dirty mode if
+     * there's outstanding p2m_ioreq_server pages.

Grammar nit: if *there are* outstanding pages.

Oh, right. Thanks

B.R.
Yu

Quoted 2 lines

-George

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

author
#29 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to George Dunlap
Quoted 30 lines · George Dunlap
Quoted 29 lines · Yu Zhang
Quoted 17 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

Quoted 7 lines

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

Quoted 8 lines
Quoted 3 lines

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw before being set to p2m_invalid, just like the normal path. Will this be a problem?

Quoted 13 lines

It's the hypervisor's job to do the right thing even if the guest and the device model don't.

Quoted 10 lines

And even when emulation is finished, the balloon driver successfully get this page, and triggers
decrease_reservation, the purpose is to remove the current mapping relation between the gfn
and mfn in p2m. So IIUC, p2m_remove_page() will be triggered if everything is goes fine, and then
p2m_set_entry(), which will trigger the recalc logic eventually, either in ept_set_entry() or
p2m_pt_set_entry(). Then the entry_count will be updated in the recalc logic.

Quoted 2 lines

Yes, once the lazy type change has been made, we can rely on the recalculation logic to make sure that the types are changed appropriately.

Yep. So my understanding is that as long as p2m_set_entry() is used to change the p2m, we do not need to worry about this.

B.R.
Yu

Quoted 4 lines

-George

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

#28 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 26 lines · Yu Zhang
Quoted 23 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Quoted 8 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

It's the hypervisor's job to do the right thing even if the guest and the device model don't.

Quoted 10 lines

And even when emulation is finished, the balloon driver successfully get this page, and triggers
decrease_reservation, the purpose is to remove the current mapping relation between the gfn
and mfn in p2m. So IIUC, p2m_remove_page() will be triggered if everything is goes fine, and then
p2m_set_entry(), which will trigger the recalc logic eventually, either in ept_set_entry() or
p2m_pt_set_entry(). Then the entry_count will be updated in the recalc logic.

Yes, once the lazy type change has been made, we can rely on the recalculation logic to make sure that the types are changed appropriately.

-George

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

#30 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 42 lines · Yu Zhang
Quoted 30 lines · George Dunlap
Quoted 29 lines · Yu Zhang
Quoted 17 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

Quoted 7 lines

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

Quoted 8 lines
Quoted 3 lines

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Scenario 2: Bug in the kernel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest kernel tries to balloon out page B, but makes a calculation mistake and balloons out A instead; now ioreq.entry_count is wrong

Scenario 3: Off-by-one bug in devicemodel
1. Guest driver allocates pages A-D
2. dm makes a mistake and marks pages A-E as p2m_ioreq_server (one extra page)
3. guest kernel balloons out page E; now ioreq.entry_count is wrong

Scenario 4: "Leak" in devicemodel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver is done with page A, but DM forgets to reset it to p2m_ram_rw
4. Guest driver frees A to guest kernel
5. Guest kernel balloons out page A; now ioreq.entry_count is wrong

I could keep going on; there are *lots* of bugs in the driver, the kernel, or the devicemodel which could cause pages marked p2m_ioreq_server to end up being ballooned out; which under the current code would make ioreq.entry_count wrong.

It's the hypervisor's job to do the right thing even when other components have bugs in them. This is why I initially suggested keeping count in atomic_write_ept_entry() -- no matter how the entry is changed, we always know exactly how many entries of type p2m_ioreq_server we have.

-George

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

author
#31 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 40 lines · George Dunlap
Quoted 39 lines · Yu Zhang
Quoted 37 lines · George Dunlap
Quoted 24 lines · Yu Zhang
Quoted 23 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Quoted 7 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 5 lines
Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Sorry, I do not get it. Take scenario 1 for example:

Quoted 3 lines

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server

Here in step 2. the ioreq.entry_count increases;

Quoted 2 lines

3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Here in step 4. the ioreq.entry_count decreases.
Isn't this what we are expecting?

Yu

Quoted 26 lines

Scenario 2: Bug in the kernel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest kernel tries to balloon out page B, but makes a calculation mistake and balloons out A instead; now ioreq.entry_count is wrong

Scenario 3: Off-by-one bug in devicemodel
1. Guest driver allocates pages A-D
2. dm makes a mistake and marks pages A-E as p2m_ioreq_server (one extra page)
3. guest kernel balloons out page E; now ioreq.entry_count is wrong

Scenario 4: "Leak" in devicemodel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver is done with page A, but DM forgets to reset it to p2m_ram_rw
4. Guest driver frees A to guest kernel
5. Guest kernel balloons out page A; now ioreq.entry_count is wrong

I could keep going on; there are *lots* of bugs in the driver, the kernel, or the devicemodel which could cause pages marked p2m_ioreq_server to end up being ballooned out; which under the current code would make ioreq.entry_count wrong.

Quoted 8 lines

It's the hypervisor's job to do the right thing even when other components have bugs in them. This is why I initially suggested keeping count in atomic_write_ept_entry() -- no matter how the entry is changed, we always know exactly how many entries of type p2m_ioreq_server we have.

-George

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

author
#32 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 49 lines · Yu Zhang
Quoted 40 lines · George Dunlap
Quoted 39 lines · Yu Zhang
Quoted 37 lines · George Dunlap
Quoted 24 lines · Yu Zhang
Quoted 23 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Quoted 7 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 5 lines
Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Sorry, I do not get it. Take scenario 1 for example:

Quoted 8 lines
Quoted 3 lines

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server

Here in step 2. the ioreq.entry_count increases;

Quoted 2 lines

3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Here in step 4. the ioreq.entry_count decreases.

Oh. I figured out. This entry is not invalidated yet if ioreq is not unmapped. Sorry.

Quoted 34 lines

Isn't this what we are expecting?

Yu

Quoted 31 lines

Scenario 2: Bug in the kernel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest kernel tries to balloon out page B, but makes a calculation mistake and balloons out A instead; now ioreq.entry_count is wrong

Scenario 3: Off-by-one bug in devicemodel
1. Guest driver allocates pages A-D
2. dm makes a mistake and marks pages A-E as p2m_ioreq_server (one extra page)
3. guest kernel balloons out page E; now ioreq.entry_count is wrong

Scenario 4: "Leak" in devicemodel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver is done with page A, but DM forgets to reset it to p2m_ram_rw
4. Guest driver frees A to guest kernel
5. Guest kernel balloons out page A; now ioreq.entry_count is wrong

I could keep going on; there are *lots* of bugs in the driver, the kernel, or the devicemodel which could cause pages marked p2m_ioreq_server to end up being ballooned out; which under the current code would make ioreq.entry_count wrong.

It's the hypervisor's job to do the right thing even when other components have bugs in them. This is why I initially suggested keeping count in atomic_write_ept_entry() -- no matter how the entry is changed, we always know exactly how many entries of type p2m_ioreq_server we have.

Well, count in atomic_write_ept_entry() only works for ept. Besides, it requires interface changes - need to pass the p2m.
Another thought is - now in XenGT, PoD is disabled to make sure gfn->mfn does not change. So how about we disable ballooning if ioreq.entry_count is not 0?

Yu

Quoted 9 lines
Quoted 3 lines

-George

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

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

#39 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 59 lines · Yu Zhang
Quoted 47 lines · George Dunlap
Quoted 46 lines · Yu Zhang
Quoted 44 lines · George Dunlap
Quoted 29 lines · Yu Zhang
Quoted 28 lines · George Dunlap
Quoted 5 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

Quoted 8 lines

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because
all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for
2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only
ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server
page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did
I miss something?

Quoted 7 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS
makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or
forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 5 lines
Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Sorry, I do not get it. Take scenario 1 for example:

Quoted 3 lines

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server

Quoted 6 lines

Here in step 2. the ioreq.entry_count increases;

Quoted 2 lines

3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Here in step 4. the ioreq.entry_count decreases.
Isn't this what we are expecting?

So step 4 happens via xen/common/memory.c:decrease_reservation(). Where along that path will ioreq.entry_count be decreased for the page freed?

Maybe it is, but I didn't see it when I looked. As far as I can tell, after step 4, ioreq.entry_count would still be 1, but that the actual number of p2m_ioreq_server entries would be 0.

-George

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

author
#33 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 98 lines · Yu Zhang
Quoted 49 lines · Yu Zhang
Quoted 40 lines · George Dunlap
Quoted 39 lines · Yu Zhang
Quoted 37 lines · George Dunlap
Quoted 24 lines · Yu Zhang
Quoted 23 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Quoted 7 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 5 lines
Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Sorry, I do not get it. Take scenario 1 for example:

Quoted 8 lines
Quoted 3 lines

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server

Here in step 2. the ioreq.entry_count increases;

Quoted 2 lines

3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Here in step 4. the ioreq.entry_count decreases.

Oh. I figured out. This entry is not invalidated yet if ioreq is not unmapped. Sorry.

Quoted 34 lines

Isn't this what we are expecting?

Yu

Quoted 31 lines

Scenario 2: Bug in the kernel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest kernel tries to balloon out page B, but makes a calculation mistake and balloons out A instead; now ioreq.entry_count is wrong

Scenario 3: Off-by-one bug in devicemodel
1. Guest driver allocates pages A-D
2. dm makes a mistake and marks pages A-E as p2m_ioreq_server (one extra page)
3. guest kernel balloons out page E; now ioreq.entry_count is wrong

Scenario 4: "Leak" in devicemodel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver is done with page A, but DM forgets to reset it to p2m_ram_rw
4. Guest driver frees A to guest kernel
5. Guest kernel balloons out page A; now ioreq.entry_count is wrong

I could keep going on; there are *lots* of bugs in the driver, the kernel, or the devicemodel which could cause pages marked p2m_ioreq_server to end up being ballooned out; which under the current code would make ioreq.entry_count wrong.

It's the hypervisor's job to do the right thing even when other components have bugs in them. This is why I initially suggested keeping count in atomic_write_ept_entry() -- no matter how the entry is changed, we always know exactly how many entries of type p2m_ioreq_server we have.

Quoted 4 lines

Well, count in atomic_write_ept_entry() only works for ept. Besides, it requires interface changes - need to pass the p2m.
Another thought is - now in XenGT, PoD is disabled to make sure gfn->mfn does not change. So how about we disable ballooning if ioreq.entry_count is not 0?

Or maybe just change the p2m_ioreq_server to p2m_ram_rw before it is set to p2m_invalid? Like below code:

xen/common/memory.c +0 -0

index 7dbddda..40e5f63 100644

--- a/xen/common/memory.c

+++ b/xen/common/memory.c

@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)

put_gfn(d, gmfn);

return 1;

}

+if ( unlikely(p2mt == p2m_ioreq_server) )

+p2m_change_type_one(d, gmfn,

+p2m_ioreq_server, p2m_ram_rw);

+

#else

mfn = gfn_to_mfn(d, _gfn(gmfn));

#endif

Yu

Quoted 13 lines

Yu

Quoted 9 lines
Quoted 3 lines

-George

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

Quoted 4 lines

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

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

author
#34 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 134 lines · Yu Zhang
Quoted 98 lines · Yu Zhang
Quoted 49 lines · Yu Zhang
Quoted 40 lines · George Dunlap
Quoted 39 lines · Yu Zhang
Quoted 37 lines · George Dunlap
Quoted 24 lines · Yu Zhang
Quoted 23 lines · George Dunlap
Quoted 11 lines · Yu Zhang

After an ioreq server has unmapped, the remaining p2m_ioreq_server entries need to be reset back to p2m_ram_rw. This patch does this asynchronously with the current p2m_change_entry_type_global() interface.

New field entry_count is introduced in struct p2m_domain, to record the number of p2m_ioreq_server p2m page table entries. One nature of these entries is that they only point to 4K sized page frames, because all p2m_ioreq_server entries are originated from p2m_ram_rw ones in p2m_change_type_one(). We do not need to worry about the counting for 2M/1G sized pages.

Assuming that all p2m_ioreq_server entries are *created* by p2m_change_type_one() may valid, but can you assume that they are only ever *removed* by p2m_change_type_one() (or recalculation)?

What happens, for instance, if a guest balloons out one of the ram pages? I don't immediately see anything preventing a p2m_ioreq_server page from being ballooned out, nor anything on the decrease_reservation() path decreasing p2m->ioreq.entry_count. Or did I miss something?

Quoted 7 lines
Quoted 1 line

Other than that, only one minor comment...

Thanks for your thorough consideration, George. But I do not think we need to worry about this:

If the emulation is in process, the balloon driver cannot get a p2m_ioreq_server page - because
it is already allocated.

In theory, yes, the guest *shouldn't* do this. But what if the guest OS makes a mistake? Or, what if the ioreq server makes a mistake and places a watch on a page that *isn't* allocated by the device driver, or forgets to change a page type back to ram when the device driver frees it back to the guest kernel?

Quoted 5 lines
Quoted 4 lines

Then the lazy p2m change code will be triggered, and this page is reset to p2m_ram_rw
before being set to p2m_invalid, just like the normal path. Will this be a problem?

No, I'm talking about before the ioreq server detaches.

Sorry, I do not get it. Take scenario 1 for example:

Quoted 8 lines
Quoted 3 lines

Scenario 1: Bug in driver
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server

Here in step 2. the ioreq.entry_count increases;

Quoted 2 lines

3. Guest driver accidentally frees A to the kernel
4. guest kernel balloons out page A; ioreq.entry_count is wrong

Here in step 4. the ioreq.entry_count decreases.

Oh. I figured out. This entry is not invalidated yet if ioreq is not unmapped. Sorry.

Quoted 34 lines

Isn't this what we are expecting?

Yu

Quoted 31 lines

Scenario 2: Bug in the kernel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest kernel tries to balloon out page B, but makes a calculation mistake and balloons out A instead; now ioreq.entry_count is wrong

Scenario 3: Off-by-one bug in devicemodel
1. Guest driver allocates pages A-D
2. dm makes a mistake and marks pages A-E as p2m_ioreq_server (one extra page)
3. guest kernel balloons out page E; now ioreq.entry_count is wrong

Scenario 4: "Leak" in devicemodel
1. Guest driver allocates page A
2. dm marks A as p2m_ioreq_server
3. Guest driver is done with page A, but DM forgets to reset it to p2m_ram_rw
4. Guest driver frees A to guest kernel
5. Guest kernel balloons out page A; now ioreq.entry_count is wrong

I could keep going on; there are *lots* of bugs in the driver, the kernel, or the devicemodel which could cause pages marked p2m_ioreq_server to end up being ballooned out; which under the current code would make ioreq.entry_count wrong.

It's the hypervisor's job to do the right thing even when other components have bugs in them. This is why I initially suggested keeping count in atomic_write_ept_entry() -- no matter how the entry is changed, we always know exactly how many entries of type p2m_ioreq_server we have.

Quoted 4 lines

Well, count in atomic_write_ept_entry() only works for ept. Besides, it requires interface changes - need to pass the p2m.
Another thought is - now in XenGT, PoD is disabled to make sure gfn->mfn does not change. So how about we disable ballooning if ioreq.entry_count is not 0?

Or maybe just change the p2m_ioreq_server to p2m_ram_rw before it is set to p2m_invalid? Like below code:

xen/common/memory.c +0 -0

index 7dbddda..40e5f63 100644

--- a/xen/common/memory.c

+++ b/xen/common/memory.c

xen/common/memory.c +0 -0
@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned 
 long gmfn)
 
 put_gfn(d, gmfn);
 
 return 1;
 

}

xen/common/memory.c +4 -0
+if ( unlikely(p2mt == p2m_ioreq_server) )
 
+p2m_change_type_one(d, gmfn,
 
+p2m_ioreq_server, p2m_ram_rw);
 
+
Quoted 7 lines

#else

mfn = gfn_to_mfn(d, _gfn(gmfn));

#endif

Sorry for the format. Please ignore above code, and see below one:

xen/common/memory.c +4 -0
index 7dbddda..40e5f63 100644
@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
         put_gfn(d, gmfn);
         return 1;
     }
+    if ( unlikely(p2mt == p2m_ioreq_server) )
+        p2m_change_type_one(d, gmfn,
+                            p2m_ioreq_server, p2m_ram_rw);
+
 #else
     mfn = gfn_to_mfn(d, _gfn(gmfn));
 #endif

Yu

Quoted 21 lines

Yu

Quoted 13 lines

Yu

Quoted 9 lines
Quoted 3 lines

-George

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

Quoted 4 lines

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

Quoted 4 lines

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

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

#38 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 19:28, <[email protected]> wrote:

Well, count in atomic_write_ept_entry() only works for ept. Besides, it requires
interface changes - need to pass the p2m.
Another thought is - now in XenGT, PoD is disabled to make sure gfn->mfn does
not change. So how about we disable ballooning if ioreq.entry_count is not 0?

You can't disable ballooning - the guest is free to make decisions regarding what memory to return to the hypervisor all by itself. Tool stack directions (via xenstore) are merely a hint for well behaved guests.

Jan

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

#35 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

In reply to Yu Zhang
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 20:04, <[email protected]> wrote:

xen/common/memory.c int guest_remove_page(struct domain *d, unsigned long gmfn) · line 297

Quoted 10 lines
xen/common/memory.c +4 -0
@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
          put_gfn(d, gmfn);
          return 1;
      }
+    if ( unlikely(p2mt == p2m_ioreq_server) )
+        p2m_change_type_one(d, gmfn,
+                            p2m_ioreq_server, p2m_ram_rw);
+
  #else
      mfn = gfn_to_mfn(d, _gfn(gmfn));
  #endif

To be honest, this looks more like a quick hack than a proper solution at the first glance. To me it would seem preferable if the count was adjusted at the point the P2M entry is being replaced (i.e. down the call stack from guest_physmap_remove_page()). The closer to the actual changing of the P2M entry, the less likely you miss any call paths (as was already explained by George while suggesting to put the accounting into the leaf most EPT/NPT functions).

Jan

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

author
#36 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 16 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 20:04, <[email protected]> wrote:

xen/common/memory.c int guest_remove_page(struct domain *d, unsigned long gmfn) · line 297

Quoted 10 lines
xen/common/memory.c +4 -0
@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
           put_gfn(d, gmfn);
           return 1;
       }
+    if ( unlikely(p2mt == p2m_ioreq_server) )
+        p2m_change_type_one(d, gmfn,
+                            p2m_ioreq_server, p2m_ram_rw);
+
   #else
       mfn = gfn_to_mfn(d, _gfn(gmfn));
   #endif

To be honest, this looks more like a quick hack than a proper solution at the first glance. To me it would seem preferable if the count was

Yeah, right. :)

Quoted 5 lines

adjusted at the point the P2M entry is being replaced (i.e. down the call stack from guest_physmap_remove_page()). The closer to the actual changing of the P2M entry, the less likely you miss any call paths (as was already explained by George while suggesting to put the accounting into the leaf most EPT/NPT functions).

Well, I thought I have explained the reason why I have always been hesitating to do the count in atomic_write_ept_entry(). But seems I did not make it clear:

1> atomic_write_ept_entry() is used each time an p2m entry is written, but sweeping p2m_ioreq_server is only supposed to happen when an ioreq server unmaps. Checking the p2m here impacts the performance, and I do not think it is worthy - consider there may be very limited usage requirement for the p2m_ioreq_server;

2> atomic_write_ept_entry() does not have a p2m parameter, even some of its caller does not have either;

3> the lazy p2m type change is triggered at p2m level, by p2m_change_entry_type_global(). It can be used not only for intel ept. And supporting the count at the lowest level for non intel ept platform is more complex than changes in atomic_write_ept_entry().

4> Fortunately, we have both resolve_misconfig() and do_recalc(), so I thought it would be enough to do the count in these two routines, together with the count in p2m_change_type_one().

But I had to admit I did not think of the extreme scenarios raised by George - I had always assumed an p2m_ioreq_server page will not be allocated to the balloon driver when it is in use.

So here I have another proposal - we shall not allow a p2m_ioreq_server being ballooned out. I mean, if some bug in kernel really allocates a p2m_ioreq_server page to a balloon driver, or if the driver is a malicious one which does not tell the device model this gfn shall be no longer emulated, the hypervisor shall let the ballooning fail for this gfn. After all, if such situation happens, the guest or the device model already have bugs, and these last 2 patches are to make sure that even if there's bug in guest/device model, xen will help do the cleanup, instead of to tolerate guest bugs.

If you think this is reasonable, I have drafted a patch, like this:

xen/arch/x86/mm/p2m-pod.c +2 -1
index d5fea72..ff726ad 100644
@@ -606,7 +606,8 @@ p2m_pod_decrease_reservation(struct domain *d,
             BUG_ON(p2m->pod.entry_count < 0);
             pod -= n;
         }
-        else if ( steal_for_cache && p2m_is_ram(t) )
+        else if ( steal_for_cache && p2m_is_ram(t) &&
+                  (t != p2m_ioreq_server) )
         {
             /*
              * If we need less than 1 << cur_order, we may end up stealing
xen/common/memory.c +8 -0
index 7dbddda..40d5545 100644
@@ -288,6 +288,14 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
         put_gfn(d, gmfn);
         return 1;
     }
+    if ( unlikely(p2mt == p2m_ioreq_server) )
+    {
+        put_gfn(d, gmfn);
+        gdprintk(XENLOG_INFO, "Domain %u page %lx cannot be removed.\n",
+                d->domain_id, gmfn);
+        return 0;
+    }
+
 #else
     mfn = gfn_to_mfn(d, _gfn(gmfn));
 #endif

Changes made in p2m_pod_decrease_reservation() is to not let balloon driver to steal a p2m_ioreq_server page in PoD; Changes made in guest_remove_page() is to disallow a p2m_ioreq_server page being removed.

Thanks
Yu

Quoted 3 lines

Jan

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

#37 lore raw

Re: [PATCH v10 5/6] x86/ioreq server: Asynchronously reset outstanding p2m_ioreq_server entries.

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 10:27, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.04.17 at 20:04, <[email protected]> wrote:

xen/common/memory.c int guest_remove_page(struct domain *d, unsigned long gmfn) · line 297

Quoted 10 lines
xen/common/memory.c +4 -0
@@ -288,6 +288,10 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
           put_gfn(d, gmfn);
           return 1;
       }
+    if ( unlikely(p2mt == p2m_ioreq_server) )
+        p2m_change_type_one(d, gmfn,
+                            p2m_ioreq_server, p2m_ram_rw);
+
   #else
       mfn = gfn_to_mfn(d, _gfn(gmfn));
   #endif

To be honest, this looks more like a quick hack than a proper solution at the first glance. To me it would seem preferable if the count was

Yeah, right. :)

Quoted 5 lines

adjusted at the point the P2M entry is being replaced (i.e. down the call stack from guest_physmap_remove_page()). The closer to the actual changing of the P2M entry, the less likely you miss any call paths (as was already explained by George while suggesting to put the accounting into the leaf most EPT/NPT functions).

Quoted 3 lines

Well, I thought I have explained the reason why I have always been hesitating to do the count in
atomic_write_ept_entry(). But seems I did not make it clear:

Well, there was no reason to re-explain. I understand your reasoning, and I understand George's. Hence my request to move it _closer_ to the leaf function, not specifically to move it _into_ there.

Quoted 15 lines

But I had to admit I did not think of the extreme scenarios raised by George - I had always assumed an p2m_ioreq_server
page will not be allocated to the balloon driver when it is in use.

So here I have another proposal - we shall not allow a p2m_ioreq_server being ballooned out. I mean, if some bug in
kernel really allocates a p2m_ioreq_server page to a balloon driver, or if the driver is a malicious one which does not
tell the device model this gfn shall be no longer emulated, the hypervisor shall let the ballooning fail for this gfn. After all, if such situation happens, the guest or the device model already have bugs, and these last 2 patches are to make
sure that even if there's bug in guest/device model, xen will help do the cleanup, instead of to tolerate guest bugs.

Quoted 1 line

If you think this is reasonable, I have drafted a patch, like this:

Well, that's still the same hack as before (merely extended to PoD code), isn't it? I'd still prefer the accounting to be got right instead of the page remove attempt to be failed.

Jan

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