Skip to contentSkip to conversation

Add V4V to Xen (v6)

PATCH In review Jean Guyader 5 participants 18 messages Sep 20, 2012 – Oct 8, 2012 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

cover Add V4V to Xen (v6)

v6 changes:
- Check compiler before using [0] or [].

v5 changes:
- Fix prototypes in v4v.h for do_v4v_op
- Add padding to make sure all the structures are 64 bits aligned
- Replace [0] with []

v4 changes:
- Stop using ssize_t, use long instead
- Return -MSGSIZE for message bigger than 2GB
- Fixup size handling
- Rename protocol with message_type to avoid the confusion with the IP protocol flag
- Replaced V4V_DOMID_ANY value with DOMID_INVALID
- Replaced v4v_pfn_t with xen_pfn_t
- Add padding to struct v4v_info
- Fixup hypercall documentation
- Move V4V_ROUNDUP to v4v.c
- Remove v4v_utils.h (we could potentially add it later).

v3 changes:
- Switch to event channel
- Allocated a unbound event channel
per domain.
- Add a new v4v call to share the
event channel port.
- Public headers with actual type definition
- Align all the v4v type to 64 bits
- Modify v4v MAGIC numbers because we won't
but backward compatible anymore
- Merge insert and insertv
- Merge send and sendv
- Turn all the lock prerequisite from comment
to ASSERT()
- Make use or write_atomic instead of volatile pointers
- Merge v4v_memcpy_to_guest_ring and v4v_memcpy_to_guest_ring_from_guest
- Introduce copy_from_guest_maybe that can take a void * and a handle as src address.
- Replace 6 arguments hypercalls with 5 arguments hypercalls.

v2 changes:
- Cleanup plugin header
- Include basic access control
- Use guest_handle_for_field

Jan Beulich (1):
xen: Introduce guest_handle_for_field

Jean Guyader (3):
xen: virq, remove VIRQ_XC_RESERVED
xen: events, exposes evtchn_alloc_unbound_domain
xen: Add V4V implementation

 xen/arch/x86/hvm/hvm.c             |    6 +-
 xen/arch/x86/x86_64/compat/entry.S |    2 +
 xen/arch/x86/x86_64/entry.S        |    2 +
 xen/common/Makefile                |    1 +
 xen/common/domain.c                |   13 +-
 xen/common/event_channel.c         |   39 +-
 xen/common/v4v.c                   | 1905 ++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/guest_access.h |    3 +
 xen/include/public/v4v.h           |  324 ++++++
 xen/include/public/xen.h           |    3 +-
 xen/include/xen/event.h            |    3 +
 xen/include/xen/sched.h            |    4 +
 xen/include/xen/v4v.h              |  133 +++
 13 files changed, 2421 insertions(+), 17 deletions(-)
 create mode 100644 xen/common/v4v.c
 create mode 100644 xen/include/public/v4v.h
 create mode 100644 xen/include/xen/v4v.h
Signature · 1 line
1.7.9.5

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

#1 lore raw

PATCH 1/4 xen: Introduce guest_handle_for_field

This helper turns a field of a GUEST_HANDLE in
a GUEST_HANDLE.

---
 xen/include/asm-x86/guest_access.h |    3 +++
 1 file changed, 3 insertions(+)
xen/include/asm-x86/guest_access.h +3 -0
index 2b429c2..e3ac1d6 100644
@@ -51,6 +51,9 @@
     (XEN_GUEST_HANDLE(type)) { _x };            \
 })
 
+#define guest_handle_for_field(hnd, type, fld)          \
+    ((XEN_GUEST_HANDLE(type)) { &(hnd).p->fld })
+
 #define guest_handle_from_ptr(ptr, type)        \
     ((XEN_GUEST_HANDLE(type)) { (type *)ptr })
 #define const_guest_handle_from_ptr(ptr, type)  \

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

#2 lore raw

PATCH 2/4 xen: virq, remove VIRQ_XC_RESERVED

In reply to Jean Guyader

VIRQ_XC_RESERVED was reserved for V4V but we have switched to event channels so this place holder is no longer required.

---
 xen/include/public/xen.h |    1 -
 1 file changed, 1 deletion(-)
xen/include/public/xen.h +0 -1
index 361398b..8def420 100644
@@ -155,7 +155,6 @@ DEFINE_XEN_GUEST_HANDLE(xen_pfn_t);
 #define VIRQ_CON_RING   8  /* G. (DOM0) Bytes received on console            */
 #define VIRQ_PCPU_STATE 9  /* G. (DOM0) PCPU state changed                   */
 #define VIRQ_MEM_EVENT  10 /* G. (DOM0) A memory event has occured           */
-#define VIRQ_XC_RESERVED 11 /* G. Reserved for XenClient                     */
 #define VIRQ_ENOMEM     12 /* G. (DOM0) Low on heap memory       */
 
 /* Architecture-specific VIRQ definitions. */

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

#3 lore raw

PATCH 3/4 xen: events, exposes evtchn_alloc_unbound_domain

In reply to Jean Guyader

Exposes evtchn_alloc_unbound_domain to the rest of Xen so we can create allocated unbound evtchn within Xen.

---
 xen/common/event_channel.c |   39 +++++++++++++++++++++++++++------------
 xen/include/xen/event.h    |    3 +++
 2 files changed, 30 insertions(+), 12 deletions(-)
xen/common/event_channel.c +27 -12
index 53777f8..6a0e342 100644
@@ -159,36 +159,51 @@ static int get_free_port(struct domain *d)
 
 static long evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
 {
-    struct evtchn *chn;
     struct domain *d;
-    int            port;
-    domid_t        dom = alloc->dom;
     long           rc;
 
-    rc = rcu_lock_target_domain_by_id(dom, &d);
+    rc = rcu_lock_target_domain_by_id(alloc->dom, &d);
     if ( rc )
         return rc;
 
+    rc = evtchn_alloc_unbound_domain(d, &alloc->port, alloc->remote_dom);
+    if ( rc )
+        ERROR_EXIT_DOM((int)rc, d);
+
+ out:
+    rcu_unlock_domain(d);
+
+    return rc;
+}
+
+int evtchn_alloc_unbound_domain(struct domain *d, evtchn_port_t *port,
+                                domid_t remote_domid)
+{
+    struct evtchn *chn;
+    int           rc;
+    int           free_port;
+
     spin_lock(&d->event_lock);
 
-    if ( (port = get_free_port(d)) < 0 )
-        ERROR_EXIT_DOM(port, d);
-    chn = evtchn_from_port(d, port);
+    rc = free_port = get_free_port(d);
+    if ( free_port < 0 )
+        goto out;
 
-    rc = xsm_evtchn_unbound(d, chn, alloc->remote_dom);
+    chn = evtchn_from_port(d, free_port);
+    rc = xsm_evtchn_unbound(d, chn, remote_domid);
     if ( rc )
         goto out;
 
     chn->state = ECS_UNBOUND;
-    if ( (chn->u.unbound.remote_domid = alloc->remote_dom) == DOMID_SELF )
+    if ( (chn->u.unbound.remote_domid = remote_domid) == DOMID_SELF )
         chn->u.unbound.remote_domid = current->domain->domain_id;
 
-    alloc->port = port;
+    *port = free_port;
+    /* Everything is fine, returns 0 */
+    rc = 0;
 
  out:
     spin_unlock(&d->event_lock);
-    rcu_unlock_domain(d);
-
     return rc;
 }
 
xen/include/xen/event.h +3 -0
index 71c3e92..1a0c832 100644
@@ -69,6 +69,9 @@ int guest_enabled_event(struct vcpu *v, uint32_t virq);
 /* Notify remote end of a Xen-attached event channel.*/
 void notify_via_xen_event_channel(struct domain *ld, int lport);
 
+int evtchn_alloc_unbound_domain(struct domain *d, evtchn_port_t *port,
+                                domid_t remote_domid);
+
 /* Internal event channel object accessors */
 #define bucket_from_port(d,p) \
     ((d)->evtchn[(p)/EVTCHNS_PER_BUCKET])

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

#4 lore raw

PATCH 4/4 xen: Add V4V implementation

In reply to Jean Guyader

Setup of v4v domains a domain gets created and cleanup when a domain die. Wire up the v4v hypercall.

Include v4v internal and public headers.

---
 xen/arch/x86/hvm/hvm.c             |    6 +-
 xen/arch/x86/x86_64/compat/entry.S |    2 +
 xen/arch/x86/x86_64/entry.S        |    2 +
 xen/common/Makefile                |    1 +
 xen/common/domain.c                |   13 +-
 xen/common/v4v.c                   | 1905 ++++++++++++++++++++++++++++++++++++
 xen/include/public/v4v.h           |  324 ++++++
 xen/include/public/xen.h           |    2 +-
 xen/include/xen/sched.h            |    4 +
 xen/include/xen/v4v.h              |  133 +++
 10 files changed, 2388 insertions(+), 4 deletions(-)
 create mode 100644 xen/common/v4v.c
 create mode 100644 xen/include/public/v4v.h
 create mode 100644 xen/include/xen/v4v.h
xen/arch/x86/hvm/hvm.c +4 -2
index d69e419..b685535 100644
@@ -3183,7 +3183,8 @@ static hvm_hypercall_t *const hvm_hypercall64_table[NR_hypercalls] = {
     HYPERCALL(set_timer_op),
     HYPERCALL(hvm_op),
     HYPERCALL(sysctl),
-    HYPERCALL(tmem_op)
+    HYPERCALL(tmem_op),
+    HYPERCALL(v4v_op)
 };
 
 #define COMPAT_CALL(x)                                        \
@@ -3200,7 +3201,8 @@ static hvm_hypercall_t *const hvm_hypercall32_table[NR_hypercalls] = {
     COMPAT_CALL(set_timer_op),
     HYPERCALL(hvm_op),
     HYPERCALL(sysctl),
-    HYPERCALL(tmem_op)
+    HYPERCALL(tmem_op),
+    HYPERCALL(v4v_op)
 };
 
 int hvm_do_hypercall(struct cpu_user_regs *regs)
xen/arch/x86/x86_64/compat/entry.S +2 -0
index 2f606ab..f518a03 100644
@@ -414,6 +414,7 @@ ENTRY(compat_hypercall_table)
         .quad do_domctl
         .quad compat_kexec_op
         .quad do_tmem_op
+        .quad do_v4v_op
         .rept __HYPERVISOR_arch_0-((.-compat_hypercall_table)/8)
         .quad compat_ni_hypercall
         .endr
@@ -462,6 +463,7 @@ ENTRY(compat_hypercall_args_table)
         .byte 1 /* do_domctl                */
         .byte 2 /* compat_kexec_op          */
         .byte 1 /* do_tmem_op               */
+        .byte 5 /* do_v4v_op		    */
         .rept __HYPERVISOR_arch_0-(.-compat_hypercall_args_table)
         .byte 0 /* compat_ni_hypercall      */
         .endr
xen/arch/x86/x86_64/entry.S +2 -0
index 8156827..5f4e7bf 100644
@@ -707,6 +707,7 @@ ENTRY(hypercall_table)
         .quad do_domctl
         .quad do_kexec_op
         .quad do_tmem_op
+        .quad do_v4v_op
         .rept __HYPERVISOR_arch_0-((.-hypercall_table)/8)
         .quad do_ni_hypercall
         .endr
@@ -755,6 +756,7 @@ ENTRY(hypercall_args_table)
         .byte 1 /* do_domctl            */
         .byte 2 /* do_kexec             */
         .byte 1 /* do_tmem_op           */
+        .byte 5 /* do_v4v_op		*/
         .rept __HYPERVISOR_arch_0-(.-hypercall_args_table)
         .byte 0 /* do_ni_hypercall      */
         .endr
xen/common/Makefile +1 -0
index c1c100f..ba63cec 100644
@@ -45,6 +45,7 @@ obj-y += tmem_xen.o
 obj-y += radix-tree.o
 obj-y += rbtree.o
 obj-y += lzo.o
+obj-y += v4v.o
 
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo,$(n).init.o)
 
xen/common/domain.c +12 -1
index a1aa05e..94283e3 100644
@@ -195,7 +195,8 @@ struct domain *domain_create(
 {
     struct domain *d, **pd;
     enum { INIT_xsm = 1u<<0, INIT_watchdog = 1u<<1, INIT_rangeset = 1u<<2,
-           INIT_evtchn = 1u<<3, INIT_gnttab = 1u<<4, INIT_arch = 1u<<5 };
+           INIT_evtchn = 1u<<3, INIT_gnttab = 1u<<4, INIT_arch = 1u<<5,
+           INIT_v4v = 1u<<6 };
     int err, init_status = 0;
     int poolid = CPUPOOLID_NONE;
 
@@ -307,6 +308,13 @@ struct domain *domain_create(
         spin_unlock(&domlist_update_lock);
     }
 
+    if ( !is_idle_domain(d) )
+    {
+        if ( v4v_init(d) != 0 )
+            goto fail;
+        init_status |= INIT_v4v;
+    }
+
     return d;
 
  fail:
@@ -315,6 +323,8 @@ struct domain *domain_create(
     xfree(d->mem_event);
     if ( init_status & INIT_arch )
         arch_domain_destroy(d);
+    if ( init_status & INIT_v4v )
+	v4v_destroy(d);
     if ( init_status & INIT_gnttab )
         grant_table_destroy(d);
     if ( init_status & INIT_evtchn )
@@ -468,6 +478,7 @@ int domain_kill(struct domain *d)
         domain_pause(d);
         d->is_dying = DOMDYING_dying;
         spin_barrier(&d->domain_lock);
+        v4v_destroy(d);
         evtchn_destroy(d);
         gnttab_release_mappings(d);
         tmem_destroy(d->tmem);
xen/common/v4v.c new file +1905 -0

1,905 lines not on this page · raw message

xen/include/public/v4v.h new file +324 -0

324 lines not on this page · raw message

xen/include/public/xen.h +1 -1
index 8def420..7a3fade 100644
@@ -97,7 +97,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_pfn_t);
 #define __HYPERVISOR_domctl               36
 #define __HYPERVISOR_kexec_op             37
 #define __HYPERVISOR_tmem_op              38
-#define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
+#define __HYPERVISOR_v4v_op               39
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48
xen/include/xen/sched.h +4 -0
index 53804c8..296de52 100644
@@ -23,6 +23,7 @@
 #include <public/sysctl.h>
 #include <public/vcpu.h>
 #include <public/mem_event.h>
+#include <xen/v4v.h>
 
 #ifdef CONFIG_COMPAT
 #include <compat/vcpu.h>
@@ -350,6 +351,9 @@ struct domain
     nodemask_t node_affinity;
     unsigned int last_alloc_node;
     spinlock_t node_affinity_lock;
+
+    /* v4v */
+    struct v4v_domain *v4v;
 };
 
 struct domain_setup_info
xen/include/xen/v4v.h new file +133 -0

133 lines not on this page · raw message

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

#5 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

xen/common/v4v.c line 1617

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

xen/common/v4v.c line 1643

Quoted 4 lines
xen/common/v4v.c new file +4 -0
+                if ( unlikely(!guest_handle_okay(addr_hnd, 1)) )
+                    goto out;
+                if ( copy_from_guest(&addr, addr_hnd, 1) )
+                    goto out;

The former is redundant with the latter. Also, if you already used guest_handle_okay() on an array, you then can (and should) use the cheaper __copy_{to,from}_guest() functions. While looking at this, I also spotted at least on guest access without error checking. Plus many guest accesses happen with some sort of lock held - that'll cause problems when the guest memory you're trying to access was paged out (quite likely you would be able to point out other such cases, but those likely predate paging, and hence are an oversight of whoever introduced the paging code).

xen/include/public/v4v.h line 144

Quoted 4 lines
xen/include/public/v4v.h new file +4 -0
+struct v4v_ring_message_header
+{
+    uint32_t len;
+    uint32_t pad0;

Why? v4v_addr_t is 4-byte aligned.

xen/include/public/v4v.h line 147

Quoted 3 lines
xen/include/public/v4v.h new file +3 -0
+    v4v_addr_t source;
+    uint32_t message_type;
+    uint32_t pad1;

And again, why?

Quoted 6 lines
xen/include/public/v4v.h new file +6 -0
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+    uint8_t data[];
+#elif defined(__GNUC__)
+    uint8_t data[0];
+#endif
+};

...

xen/include/public/v4v.h line 188

Quoted 11 lines
xen/include/public/v4v.h new file +11 -0
+/*
+ * V4VOP_register_ring
+ *
+ * Registers a ring with Xen. If a ring with the same v4v_ring_id exists,
+ * the hypercall will return -EEXIST.
+ *
+ * do_v4v_op(V4VOP_unregister_ring,
+ *           XEN_GUEST_HANDLE(v4v_ring_t), XEN_GUEST_HANDLE(xen_pfn_t),
+ *           npage, 0)
+ */
+#define V4VOP_register_ring 	1

"register" or "unregister"?

Also, note the use of v4v_ring_t here, but ...

xen/include/public/v4v.h line 200

Quoted 10 lines
xen/include/public/v4v.h new file +10 -0
+/*
+ * V4VOP_unregister_ring
+ *
+ * Unregister a ring.
+ *
+ * do_v4v_op(V4VOP_unregister_ring,
+ *           XEN_GUEST_HANDLE(v4v_ring),
+ *           NULL, 0, 0)
+ */
+#define V4VOP_unregister_ring 	2

... not here. Please be consistent, even if this is just comments. (Again at least for V4VOP_sendv.)

Quoted 4 lines

+/*
+ * V4VOP_viptables_list
+ *
+ * Delete a filtering rules at a position or the rule

Delete? Also, here and above, make clear whether you talk about a single or multiple rules.

Quoted 7 lines

+ * that matches "rule".
+ *
+ * do_v4v_op(V4VOP_viptables_list,
+ * XEN_GUEST_HANDLE(v4v_vitpables_list_t) list,
+ * NULL, 0, 0)
+ */
+#define V4VOP_viptables_list 8

Also, with all of the padding fields and unused arguments (for certain sub-ops) - if you see the slightest chance of them ever getting used for some extension, you will want to make sure they got zero-filled by the guest.

Jan

#6 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 13 lines · Jan Beulich

xen/common/v4v.c line 1617

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;
Quoted 3 lines

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

Quoted 5 lines

xen/common/v4v.c line 1643

Quoted 4 lines
xen/common/v4v.c new file +4 -0
+                if ( unlikely(!guest_handle_okay(addr_hnd, 1)) )
+                    goto out;
+                if ( copy_from_guest(&addr, addr_hnd, 1) )
+                    goto out;
Quoted 11 lines

The former is redundant with the latter. Also, if you already used guest_handle_okay() on an array, you then can (and should) use the cheaper __copy_{to,from}_guest() functions. While looking at this, I also spotted at least on guest access without error checking. Plus many guest accesses happen with some sort of lock held - that'll cause problems when the guest memory you're trying to access was paged out (quite likely you would be able to point out other such cases, but those likely predate paging, and hence are an oversight of whoever introduced the paging code).

There are plenty of other place in Xen where we copy data from a guest with some sort of lock held.

I understand that the new code should do it's best to make sure it works correctly with the Xen paging code.

The best way to solve this might not be try to to avoid copying from guest memory under a lock. I've discussed this with Tim and maybe we could introduce a new copy_from_guest which is aware of the paging and returns a specific error, and then we could cleanly unlock everything and issue a continuation that will fixup the memory.

Quoted 66 lines

xen/include/public/v4v.h line 144

Quoted 4 lines
xen/include/public/v4v.h new file +4 -0
+struct v4v_ring_message_header
+{
+    uint32_t len;
+    uint32_t pad0;

Why? v4v_addr_t is 4-byte aligned.

xen/include/public/v4v.h line 147

Quoted 3 lines
xen/include/public/v4v.h new file +3 -0
+    v4v_addr_t source;
+    uint32_t message_type;
+    uint32_t pad1;

And again, why?

Quoted 6 lines
xen/include/public/v4v.h new file +6 -0
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+    uint8_t data[];
+#elif defined(__GNUC__)
+    uint8_t data[0];
+#endif
+};

...

xen/include/public/v4v.h line 188

Quoted 11 lines
xen/include/public/v4v.h new file +11 -0
+/*
+ * V4VOP_register_ring
+ *
+ * Registers a ring with Xen. If a ring with the same v4v_ring_id exists,
+ * the hypercall will return -EEXIST.
+ *
+ * do_v4v_op(V4VOP_unregister_ring,
+ *           XEN_GUEST_HANDLE(v4v_ring_t), XEN_GUEST_HANDLE(xen_pfn_t),
+ *           npage, 0)
+ */
+#define V4VOP_register_ring   1

"register" or "unregister"?

Also, note the use of v4v_ring_t here, but ...

xen/include/public/v4v.h line 200

Quoted 10 lines
xen/include/public/v4v.h new file +10 -0
+/*
+ * V4VOP_unregister_ring
+ *
+ * Unregister a ring.
+ *
+ * do_v4v_op(V4VOP_unregister_ring,
+ *           XEN_GUEST_HANDLE(v4v_ring),
+ *           NULL, 0, 0)
+ */
+#define V4VOP_unregister_ring         2

... not here. Please be consistent, even if this is just comments. (Again at least for V4VOP_sendv.)

Quoted 4 lines

+/*
+ * V4VOP_viptables_list
+ *
+ * Delete a filtering rules at a position or the rule

Delete? Also, here and above, make clear whether you talk about a single or multiple rules.

Quoted 7 lines

+ * that matches "rule".
+ *
+ * do_v4v_op(V4VOP_viptables_list,
+ * XEN_GUEST_HANDLE(v4v_vitpables_list_t) list,
+ * NULL, 0, 0)
+ */
+#define V4VOP_viptables_list 8

Quoted 5 lines

Also, with all of the padding fields and unused arguments (for certain sub-ops) - if you see the slightest chance of them ever getting used for some extension, you will want to make sure they got zero-filled by the guest.

Thanks for the review,
Jean

#7 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 15 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 13 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;
Quoted 7 lines
Quoted 3 lines

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

Quoted 20 lines
Quoted 5 lines

xen/common/v4v.c line 1643

Quoted 4 lines
xen/common/v4v.c new file +4 -0
+                if ( unlikely(!guest_handle_okay(addr_hnd, 1)) )
+                    goto out;
+                if ( copy_from_guest(&addr, addr_hnd, 1) )
+                    goto out;
Quoted 11 lines

The former is redundant with the latter. Also, if you already used guest_handle_okay() on an array, you then can (and should) use the cheaper __copy_{to,from}_guest() functions. While looking at this, I also spotted at least on guest access without error checking. Plus many guest accesses happen with some sort of lock held - that'll cause problems when the guest memory you're trying to access was paged out (quite likely you would be able to point out other such cases, but those likely predate paging, and hence are an oversight of whoever introduced the paging code).

There are plenty of other place in Xen where we copy data from a guest with some sort of lock held.

Quoted 8 lines

I understand that the new code should do it's best to make sure it works correctly with the Xen paging code.

The best way to solve this might not be try to to avoid copying from guest memory under a lock. I've discussed this with Tim and maybe we could introduce a new copy_from_guest which is aware of the paging and returns a specific error, and then we could cleanly unlock everything and issue a continuation that will fixup the memory.

That's certainly an alternative.

Jan

#8 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 49 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 13 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;
Quoted 7 lines
Quoted 3 lines

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

Quoted 23 lines
Quoted 5 lines

xen/common/v4v.c line 1643

Quoted 4 lines
xen/common/v4v.c new file +4 -0
+                if ( unlikely(!guest_handle_okay(addr_hnd, 1)) )
+                    goto out;
+                if ( copy_from_guest(&addr, addr_hnd, 1) )
+                    goto out;
Quoted 11 lines

The former is redundant with the latter. Also, if you already used guest_handle_okay() on an array, you then can (and should) use the cheaper __copy_{to,from}_guest() functions. While looking at this, I also spotted at least on guest access without error checking. Plus many guest accesses happen with some sort of lock held - that'll cause problems when the guest memory you're trying to access was paged out (quite likely you would be able to point out other such cases, but those likely predate paging, and hence are an oversight of whoever introduced the paging code).

There are plenty of other place in Xen where we copy data from a guest with some sort of lock held.

I understand that the new code should do it's best to make sure it works correctly with the Xen paging code.

Quoted 8 lines
Quoted 5 lines

The best way to solve this might not be try to to avoid copying from guest memory under a lock. I've discussed this with Tim and maybe we could introduce a new copy_from_guest which is aware of the paging and returns a specific error, and then we could cleanly unlock everything and issue a continuation that will fixup the memory.

That's certainly an alternative.

Another way (probably a easier way) would be to add a "mlock" mechanism.

Some of the pages could be lock which mean that we know before hand that a copy_from_guest won't raise a paging error.

In my case it would be ideal. I can comment it out in the register/unregister ring
code, so when we have such mechanism in place the v4v code could be updated.

Jean

#9 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

In reply to Jan Beulich
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 16 lines · Jan Beulich

xen/common/v4v.c line 1617

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

Quoted 7 lines
Quoted 3 lines

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

[Resent, CCing everyone this time]

I'm sorry, I don't really get what you mean them. I've tried to get all my struct
layout such as all the offset for the field are the same for 64b and 32b, this way I thought I could get away with doing a compat wrapper.

Thanks,
Jean

#10 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 28 lines · Jean Guyader
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 16 lines · Jan Beulich

xen/common/v4v.c line 1617

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

Quoted 7 lines
Quoted 3 lines

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

Quoted 6 lines

[Resent, CCing everyone this time]

I'm sorry, I don't really get what you mean them. I've tried to get all my struct
layout such as all the offset for the field are the same for 64b and 32b, this way I thought I could get away with doing a compat wrapper.

Even if the args don't need translation, compat-mode guests have different VA layouts and need different range checks (though I'm not sure why these aren't automatically adjusted based on current).

AIUI you need to use compat_handle_okay() instead of guest_handle_okay() to check the handles if is_pv_32on64_domain(current).

Tim.

#11 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 36 lines · Tim Deegan
Quoted 28 lines · Jean Guyader
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 16 lines · Jan Beulich

Review

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

Quoted 7 lines
Quoted 3 lines

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

Quoted 6 lines

[Resent, CCing everyone this time]

I'm sorry, I don't really get what you mean them. I've tried to get all my struct
layout such as all the offset for the field are the same for 64b and 32b, this way I thought I could get away with doing a compat wrapper.

Quoted 7 lines

Even if the args don't need translation, compat-mode guests have different VA layouts and need different range checks (though I'm not sure why these aren't automatically adjusted based on current).

AIUI you need to use compat_handle_okay() instead of guest_handle_okay() to check the handles if is_pv_32on64_domain(current).

How about something like that?

Jean

xen/include/asm-x86/guest_access.h +6 -3
index e3ac1d6..596d8a0 100644
@@ -104,9 +104,12 @@
  * Pre-validate a guest handle.
  * Allows use of faster __copy_* functions.
  */
-#define guest_handle_okay(hnd, nr)                      \
-    (paging_mode_external(current->domain) ||           \
-     array_access_ok((hnd).p, (nr), sizeof(*(hnd).p)))
+#define guest_handle_okay(hnd, nr)                                      \
+    (is_pv_32on64_domain(current->domain) &&                            \
+     compat_array_access_ok((hnd).p, (nr), sizeof(*(hnd).p))) ||        \
+    (!is_pv_32on64_domain(current->domain) &&                           \
+     (paging_mode_external(current->domain) ||                          \
+      array_access_ok((hnd).p, (nr), sizeof(*(hnd).p))))
 #define guest_handle_subrange_okay(hnd, first, last)    \
     (paging_mode_external(current->domain) ||           \
      array_access_ok((hnd).p + (first),                 \

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

#12 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 7 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 3 lines · Tim Deegan

AIUI you need to use compat_handle_okay() instead of guest_handle_okay() to check the handles if is_pv_32on64_domain(current).

How about something like that?

Could be done, but not by modifying guest_handle_okay() (which penalizes all its users), nor by (incorrectly) open-coding compat_handle_okay(). And neither should any such implementation use is_pv_32on64_domain() twice - use the conditional operator instead (that way you also avoid evaluating arguments twice).

So you could either introduce e.g. any_guest_handle_okay(), or switch all current users of guest_handle_okay() to, say, native_handle_okay() (perhaps with the exception of those where the compat mode wrapper source files #define guest_handle_okay() to compat_handle_okay(), which could then be dropped).

Jan

#13 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 15 lines
Quoted 8 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Jean Guyader
Quoted 3 lines · Tim Deegan

AIUI you need to use compat_handle_okay() instead of guest_handle_okay() to check the handles if is_pv_32on64_domain(current).

How about something like that?

Could be done, but not by modifying guest_handle_okay() (which penalizes all its users), nor by (incorrectly) open-coding compat_handle_okay(). And neither should any such implementation use is_pv_32on64_domain() twice - use the conditional operator instead (that way you also avoid evaluating arguments twice).

Quoted 5 lines

So you could either introduce e.g. any_guest_handle_okay(), or switch all current users of guest_handle_okay() to, say, native_handle_okay() (perhaps with the exception of those where the compat mode wrapper source files #define guest_handle_okay() to compat_handle_okay(), which could then be dropped).

Actually, after some more recalling of how all this was put together, you can use the existing guest_handle_okay() on anything that legitimately is a XEN_GUEST_HANDLE(). In particular, direct hypercall arguments can be expressed that way since they get properly zero-extended from 32 to 64 bits on the hypercall entry path. The things needing extra consideration are only handles embedded in structures - you need to either translate these handles, or validate that their upper halves are zero.

That's also one of the reasons why I didn't make the guest memory accessor/validation macros transparently handle both cases.

Jan

#14 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 30 lines · Jan Beulich
Quoted 15 lines
Quoted 8 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Jean Guyader
Quoted 3 lines · Tim Deegan

AIUI you need to use compat_handle_okay() instead of guest_handle_okay() to check the handles if is_pv_32on64_domain(current).

How about something like that?

Could be done, but not by modifying guest_handle_okay() (which penalizes all its users), nor by (incorrectly) open-coding compat_handle_okay(). And neither should any such implementation use is_pv_32on64_domain() twice - use the conditional operator instead (that way you also avoid evaluating arguments twice).

Quoted 5 lines

So you could either introduce e.g. any_guest_handle_okay(), or switch all current users of guest_handle_okay() to, say, native_handle_okay() (perhaps with the exception of those where the compat mode wrapper source files #define guest_handle_okay() to compat_handle_okay(), which could then be dropped).

Actually, after some more recalling of how all this was put together, you can use the existing guest_handle_okay() on anything that legitimately is a XEN_GUEST_HANDLE(). In particular, direct hypercall arguments can be expressed that way since they get properly zero-extended from 32 to 64 bits on the hypercall entry path. The things needing extra consideration are only handles embedded in structures - you need to either translate these handles, or validate that their upper halves are zero.

Quoted 4 lines

That's also one of the reasons why I didn't make the guest memory accessor/validation macros transparently handle both cases.

Ok, thanks for the background info.

In my hypercall I only use direct hypercall argument handle. The other handles that I use have been created with guest_handle_add_offset from the original argument handle.

Considering this, I think I might be ok.

Thanks,
Jean

A message that is not in the archive (sent off-list or not yet received): CAEBdQ92Box+2PMohpDi6Sy=PXFmP_sxYb0ZYSOCU4U3iPdocqQ@mail.gmail.com

#16 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 29 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 16 lines · Jan Beulich

xen/common/v4v.c line 1617

Quoted 12 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/common/v4v.c new file +11 -0
+        case V4VOP_register_ring:
+            {
+                XEN_GUEST_HANDLE(v4v_ring_t) ring_hnd =
+                    guest_handle_cast(arg1, v4v_ring_t);
+                XEN_GUEST_HANDLE(xen_pfn_t) pfn_hnd =
+                    guest_handle_cast(arg2, xen_pfn_t);
+                uint32_t npage = arg3;
+                if ( unlikely(!guest_handle_okay(ring_hnd, 1)) )
+                    goto out;
+                if ( unlikely(!guest_handle_okay(pfn_hnd, npage)) )
+                    goto out;

Here and below - this isn't sufficient for compat guests, or else you give them a way to point into the compat m2p table.

Quoted 7 lines
Quoted 3 lines

I'll probably switch to uint64_t for the v4v mfn list instead of using xen_pfn_t which
are unsigned long. That way I can save the need for a compat wrapper.

But that comment of yours doesn't address the problem I pointed out.

Quoted 5 lines

I'm sorry, I don't really get what you mean them. I've tried to get all my struct
layout such as all the offset for the field are the same for 64b and 32b, this
way I thought I could get away with doing a compat wrapper.

guest_handle_okay() simply isn't suitable for checking compat mode pointers - compat_handle_okay() needs to be used for them instead.

Jan

#17 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

In reply to Jean Guyader
Quoted 6 lines · Jean Guyader

Setup of v4v domains a domain gets created and cleanup when a domain die. Wire up the v4v hypercall.

Include v4v internal and public headers.

Could we rename "viptables" in "v4vtables", or something that doesn't contain "IP" in the name ? Since IP is a totally unrelated protocol, it doesn't make sense here.

#18 lore raw

Re: [PATCH 4/4] xen: Add V4V implementation

Quoted 5 lines · Julian Pidancet
Quoted 4 lines · Jean Guyader

Setup of v4v domains a domain gets created and cleanup when a domain die. Wire up the v4v hypercall.

Quoted 7 lines
Quoted 2 lines

Include v4v internal and public headers.

Could we rename "viptables" in "v4vtables", or something that doesn't contain "IP" in the name ? Since IP is a totally unrelated protocol, it doesn't make sense here.

Ok sure, I don't have any objections to that.

Jean