Skip to contentSkip to conversation

Argo: hypervisor-mediated interdomain communication

PATCH Superseded Christopher Clark 14 participants 104 messages Jan 7, 2019 – Feb 5, 2019 Page 2 of 2

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#78 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

In reply to Christopher Clark

to.On Mon, Jan 7, 2019 at 8:44 AM Christopher Clark

Quoted 181 lines · <[email protected]> wrote:

sendv operation is invoked to perform a synchronous send of buffers contained in iovs to a remote domain's registered ring.

It takes:
* A destination address (domid, port) for the ring to send to. It performs a most-specific match lookup, to allow for wildcard.
* A source address, used to inform the destination of where to reply.
* The address of an array of iovs containing the data to send
* .. and the length of that array of iovs
* and a 32-bit message type, available to communicate message context data (eg. kernel-to-kernel, separate from the application data).

If insufficient space exists in the destination ring, it will return -EAGAIN and Xen will notify the caller when sufficient space becomes available.

Accesses to the ring indices are appropriately atomic. The rings are mapped into Xen's private address space to write as needed and the mappings are retained for later use.

Fixed-size types are used in some areas within this code where caution around avoiding integer overflow is important.

Notifications are sent to guests via VIRQ and send_guest_global_virq is exposed in the change to enable argo to call it. VIRQ_ARGO_MESSAGE is claimed from the VIRQ previously reserved for this purpose (#11).

The VIRQ notification method is used rather than sending events using evtchn functions directly because:

* no current event channel type is an exact fit for the intended behaviour. ECS_IPI is closest, but it disallows migration to other VCPUs which is not necessarily a requirement for Argo.

* at the point of argo_init, allocation of an event channel is complicated by none of the guest VCPUs being initialized yet and the event channel logic expects that a valid event channel has a present VCPU.

* at the point of signalling a notification, the VIRQ logic is already
defensive: if d->vcpu[0] is NULL, the notification is just silently dropped, whereas the evtchn_send logic is not so defensive: vcpu[0] must not be NULL, otherwise a null pointer dereference occurs.

Using a VIRQ removes the need for the guest to query to determine which event channel notifications will be delivered on. This is also likely to simplify establishing future L0/L1 nested hypervisor argo communication.

---
The previous double-read of iovs from guest memory has been removed.

v2 self: use ring_info backpointer in pending_ent to maintain npending v2 feedback Jan: drop cookie, implement teardown
v2 self: pending_queue: reap stale ents when in need of space
v2 self: pending_requeue: reclaim ents for stale domains v2.feedback Jan: only override sender domid if DOMID_ANY
v2 feedback Jan: drop message from argo_message_op
v2 self: check npending vs maximum limit
v2 self: get_sanitized_ring instead of get_rx_ptr
v2 feedback v1#13 Jan: remove double read from ringbuf insert, lower MAX_IOV v2 self: make iov_count const
v2 self: iov_count : return EMSGSIZE for message too big
v2 self: OVERHAUL
v2 self: s/argo_pending_ent/pending_ent/g
v2 feedback v1#13 Roger: use OS-supplied roundup; drop from public header v1,2 feedback Jan/Roger/Paul: drop errno returning guest access functions v1 feedback Roger, Jan: drop argo prefix on static functions
v1 feedback #13 Jan: drop guest_handle_okay when using copy_from_guest
- reorder do_argo_op logic
v2 self: add _hnd suffix to iovs variable name to indicate guest handle type v2 self: replace use of XEN_GUEST_HANDLE_NULL with two existing macros

v1 #15 feedback, Jan: sendv op : s/ECONNREFUSED/ESRCH/
v1 #5 (#15) feedback Paul: sendv: use currd in do_argo_message_op
v1 #13 (#15) feedback Paul: sendv op: do/while reindent only
v1 #13 (#15) feedback Paul: sendv op: do/while: argo_ringbuf_insert to goto style v1 #13 (#15) feedback Paul: sendv op: do/while: reindent only again v1 #13 (#15) feedback Paul: sendv op: do/while : goto
v1 #15 feedback Paul: sendv op: make page var: unsigned
v1 #15 feedback Paul: sendv op: new local var for PAGE_SIZE - offset v1 #8 feedback Jan: XEN_GUEST_HANDLE : C89 compliance
v1 rebase after switching register op from pfns to page descriptors v1 self: move iov DEFINE_XEN_GUEST_HANDLE out of public header into argo.c v1 #13 (#15) feedback Paul: fix loglevel for guest-triggered messages v1 : add compat xlat.lst entries
v1 self: switched notification to send_guest_global_virq instead of event
v1: fix gprintk use for ARM as its defn dislikes split format strings
v1: init len variable to satisfy ARM compiler initialized checking v1 #13 feedback Jan: rename page var
v1:#14 feedback Jan: uint8_t* -> void*
v1: #13 feedback Jan: public namespace: prefix with xen
v1: #13 feedback Jan: blank line after case op in do_argo_message_op
v1: #15 feedback Jan: add comments explaining why the writes don't overrun
v1: self: add ASSERT to support comment that overrun cannot happen
v1: self: fail on short writes where guest manipulated the iov_lens
v1: self: rename ent id to domain_id
v1: self: add moan for iov rewrite
v1. feedback #15 Jan: require the pad bits are zero
v1. feedback #15 Jan: drop NULL check in argo_signal_domain as now using VIRQ v1. self: store domain_cookie in pending ent
v1. feedback #15 Jan: use unsigned where possible
v1. feedback Jan: use handle type for iov_base in public iov interface v1. self: log whenever visible error occurs
v1 feedback #15, Jan: drop unnecessary mb
v1 self: only update internal tx_ptr if able to return success
and update the visible tx_ptr
v1 self: log on failure to map ring to update visible tx_ptr
v1 feedback #15 Jan: add comment re: notification size policy
v1 self/Roger? remove errant space after sizeof
v1. feedback #15 Jan: require iov pad be zero
v1. self: rename iov_base to iov_hnd for handle in public iov interface
v1: feedback #15 Jan: handle upper-halves of hypercall args; changes some
types in function signatures to match.
v1: self: add dprintk to sendv
v1: self: add debug output to argo_iov_count
v1. feedback #14 Jan: blank line before return in argo_iov_count
v1 feedback #15 Jan: verify src id, not override

 xen/common/argo.c          | 653 +++++++++++++++++++++++++++++++++++++++++++++
 xen/common/event_channel.c |   2 +-
 xen/include/public/argo.h  |  60 +++++
 xen/include/public/xen.h   |   2 +-
 xen/include/xen/event.h    |   7 +
 xen/include/xlat.lst       |   2 +
 6 files changed, 724 insertions(+), 2 deletions(-)
xen/common/argo.c +23 -0
index 59ce8c4..4548435 100644
@@ -29,14 +29,21 @@
 #include <public/argo.h>
 
 #define MAX_RINGS_PER_DOMAIN            128U
+#define MAX_PENDING_PER_RING             32U
 
 /* All messages on the ring are padded to a multiple of the slot size. */
 #define ROUNDUP_MESSAGE(a) (ROUNDUP((a), XEN_ARGO_MSG_SLOT_SIZE))
 
+/* The maximum size of a message that may be sent on the largest Argo ring. */
+#define MAX_ARGO_MESSAGE_SIZE ((XEN_ARGO_MAX_RING_SIZE) - \
+        (sizeof(struct xen_argo_ring_message_header)) - ROUNDUP_MESSAGE(1))
+
 DEFINE_XEN_GUEST_HANDLE(xen_argo_addr_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_iov_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_page_descr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_register_ring_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_send_addr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_unregister_ring_t);
 
 /* Xen command line option to enable argo */
@@ -250,6 +257,14 @@ hash_index(const struct argo_ring_id *id)
 }
 
 static void
+signal_domain(struct domain *d)
+{
+    argo_dprintk("signalling domid:%d\n", d->domain_id);
+
+    send_guest_global_virq(d, VIRQ_ARGO_MESSAGE);
+}
+
+static void
 ring_unmap(struct argo_ring_info *ring_info)
 {
     unsigned int i;
@@ -342,6 +357,413 @@ update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr)
     smp_wmb();
 }
 
+static int
+memcpy_to_guest_ring(struct argo_ring_info *ring_info, uint32_t offset,
+                     const void *src, XEN_GUEST_HANDLE(uint8_t) src_hnd,
+                     uint32_t len)
+{
+    unsigned int mfns_index = offset >> PAGE_SHIFT;
+    void *dst;
+    int ret;

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 377

Quoted 10 lines
xen/common/argo.c +10 -0
+    unsigned int src_offset = 0;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    offset &= ~PAGE_MASK;
+
+    if ( (len > XEN_ARGO_MAX_RING_SIZE) || (offset > XEN_ARGO_MAX_RING_SIZE) )
+        return -EFAULT;
+
+    while ( (offset + len) > PAGE_SIZE )

I think you could map the whole ring in contiguous virtual address space and then writing to it would be much more easy, you wouldn't need to iterate with memcpy or copy_from_guest, take a look at __vmap. You could likely map this when the ring gets setup and keep it mapped for the lifetime of the ring.

Quoted 8 lines
xen/common/argo.c +8 -0
+    {
+        unsigned int head_len = PAGE_SIZE - offset;
+
+        ret = ring_map_page(ring_info, mfns_index, &dst);
+        if ( ret )
+            return ret;
+
+        if ( src )

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 395

Quoted 10 lines
xen/common/argo.c +10 -0
+        {
+            memcpy(dst + offset, src + src_offset, head_len);
+            src_offset += head_len;
+        }
+        else
+        {
+            ret = copy_from_guest(dst + offset, src_hnd, head_len) ?
+                    -EFAULT : 0;
+            if ( ret )
+                return ret;

You can simplify this to:

if ( copy_from_guest(...) )

    return -EFAULT;
Quoted 67 lines
xen/common/argo.c +67 -0
+
+            guest_handle_add_offset(src_hnd, head_len);
+        }
+
+        mfns_index++;
+        len -= head_len;
+        offset = 0;
+    }
+
+    ret = ring_map_page(ring_info, mfns_index, &dst);
+    if ( ret )
+    {
+        argo_dprintk("argo: ring (vm%u:%x vm%d) %p attempted to map page"
+                     " %d of %d\n", ring_info->id.domain_id, ring_info->id.port,
+                     ring_info->id.partner_id, ring_info, mfns_index,
+                     ring_info->nmfns);
+        return ret;
+    }
+
+    if ( src )
+        memcpy(dst + offset, src + src_offset, len);
+    else
+        ret = copy_from_guest(dst + offset, src_hnd, len) ? -EFAULT : 0;
+
+    return ret;
+}
+
+/*
+ * Use this with caution: rx_ptr is under guest control and may be bogus.
+ * See get_sanitized_ring for a safer alternative.
+ */
+static int
+get_rx_ptr(struct argo_ring_info *ring_info, uint32_t *rx_ptr)
+{
+    void *src;
+    xen_argo_ring_t *ringp;
+    int ret;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    if ( !ring_info->nmfns || ring_info->nmfns < ring_info->npage )
+        return -EINVAL;
+
+    ret = ring_map_page(ring_info, 0, &src);
+    if ( ret )
+        return ret;
+
+    ringp = (xen_argo_ring_t *)src;
+
+    *rx_ptr = read_atomic(&ringp->rx_ptr);
+
+    return 0;
+}
+
+/*
+ * get_sanitized_ring creates a modified copy of the ring pointers where
+ * the rx_ptr is rounded up to ensure it is aligned, and then ring
+ * wrap is handled. Simplifies safe use of the rx_ptr for available
+ * space calculation.
+ */
+static int
+get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info)
+{
+    uint32_t rx_ptr;
+    int ret;
+
+    ret = get_rx_ptr(ring_info, &rx_ptr);

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 472

Quoted 10 lines
xen/common/argo.c +10 -0
+    if ( ret )
+        return ret;
+
+    ring->tx_ptr = ring_info->tx_ptr;
+
+    rx_ptr = ROUNDUP_MESSAGE(rx_ptr);
+    if ( rx_ptr >= ring_info->len )
+        rx_ptr = 0;
+
+    ring->rx_ptr = rx_ptr;

Newline.

Quoted 7 lines
xen/common/argo.c +7 -0
+    return 0;
+}
+
+/*
+ * iov_count returns its count on success via an out variable to avoid
+ * potential for a negative return value to be used incorrectly
+ * (eg. coerced into an unsigned variable resulting in a large incorrect value)

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 489

Quoted 10 lines
xen/common/argo.c +10 -0
+ */
+static int
+iov_count(const xen_argo_iov_t *piov, unsigned long niov, uint32_t *count)
+{
+    uint32_t sum_iov_lens = 0;
+
+    if ( niov > XEN_ARGO_MAXIOV )
+        return -EINVAL;
+
+    while ( niov-- )

I would use a for loop here, that would remove the need to piov++, if you want to keep it quite similar:

for ( ; niov--; piov++ )

{
    ...
Quoted 35 lines
xen/common/argo.c +35 -0
+    {
+        /* valid iovs must have the padding field set to zero */
+        if ( piov->pad )
+        {
+            argo_dprintk("invalid iov: padding is not zero\n");
+            return -EINVAL;
+        }
+
+        /* check each to protect sum against integer overflow */
+        if ( piov->iov_len > XEN_ARGO_MAX_RING_SIZE )
+        {
+            argo_dprintk("invalid iov_len: too big (%u)>%llu\n",
+                         piov->iov_len, XEN_ARGO_MAX_RING_SIZE);
+            return -EINVAL;
+        }
+
+        sum_iov_lens += piov->iov_len;
+
+        /*
+         * Again protect sum from integer overflow
+         * and ensure total msg size will be within bounds.
+         */
+        if ( sum_iov_lens > MAX_ARGO_MESSAGE_SIZE )
+        {
+            argo_dprintk("invalid iov series: total message too big\n");
+            return -EMSGSIZE;
+        }
+
+        piov++;
+    }
+
+    *count = sum_iov_lens;
+
+    return 0;
+}

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 534

Quoted 10 lines
xen/common/argo.c +10 -0
+
+static int
+ringbuf_insert(struct domain *d, struct argo_ring_info *ring_info,
+               const struct argo_ring_id *src_id,
+               XEN_GUEST_HANDLE_PARAM(xen_argo_iov_t) iovs_hnd,
+               unsigned long niov, uint32_t message_type,
+               unsigned long *out_len)
+{
+    xen_argo_ring_t ring;
+    struct xen_argo_ring_message_header mh = { 0 };

No need for the 0, { } will achieve exactly the same.

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 538

Quoted 4 lines
xen/common/argo.c +4 -0
+    int32_t sp;
+    int32_t ret;
+    uint32_t len = 0;
+    xen_argo_iov_t iovs[XEN_ARGO_MAXIOV];

This seems slightly dangerous, a change of the maximum could cause stack overflow depending on the size of xen_argo_iov_t. I think you need some comment next to definition of XEN_ARGO_MAXIOV to note that increasing this could cause issues.

Quoted 88 lines
xen/common/argo.c +88 -0
+    xen_argo_iov_t *piov;
+    XEN_GUEST_HANDLE(uint8_t) NULL_hnd =
+       guest_handle_from_param(guest_handle_from_ptr(NULL, uint8_t), uint8_t);
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    ret = __copy_from_guest(iovs, iovs_hnd, niov) ? -EFAULT : 0;
+    if ( ret )
+        goto out;
+
+    /*
+     * Obtain the total size of data to transmit -- sets the 'len' variable
+     * -- and sanity check that the iovs conform to size and number limits.
+     * Enforced below: no more than 'len' bytes of guest data
+     * (plus the message header) will be sent in this operation.
+     */
+    ret = iov_count(iovs, niov, &len);
+    if ( ret )
+        goto out;
+
+    /*
+     * Size bounds check against ring size and static maximum message limit.
+     * The message must not fill the ring; there must be at least one slot
+     * remaining so we can distinguish a full ring from an empty one.
+     */
+    if ( ((ROUNDUP_MESSAGE(len) +
+            sizeof(struct xen_argo_ring_message_header)) >= ring_info->len) ||
+         (len > MAX_ARGO_MESSAGE_SIZE) )
+    {
+        ret = -EMSGSIZE;
+        goto out;
+    }
+
+    ret = get_sanitized_ring(&ring, ring_info);
+    if ( ret )
+        goto out;
+
+    argo_dprintk("ring.tx_ptr=%d ring.rx_ptr=%d ring len=%d"
+                 " ring_info->tx_ptr=%d\n",
+                 ring.tx_ptr, ring.rx_ptr, ring_info->len, ring_info->tx_ptr);
+
+    if ( ring.rx_ptr == ring.tx_ptr )
+        sp = ring_info->len;
+    else
+    {
+        sp = ring.rx_ptr - ring.tx_ptr;
+        if ( sp < 0 )
+            sp += ring_info->len;
+    }
+
+    /*
+     * Size bounds check against currently available space in the ring.
+     * Again: the message must not fill the ring leaving no space remaining.
+     */
+    if ( (ROUNDUP_MESSAGE(len) +
+            sizeof(struct xen_argo_ring_message_header)) >= sp )
+    {
+        argo_dprintk("EAGAIN\n");
+        ret = -EAGAIN;
+        goto out;
+    }
+
+    mh.len = len + sizeof(struct xen_argo_ring_message_header);
+    mh.source.port = src_id->port;
+    mh.source.domain_id = src_id->domain_id;
+    mh.message_type = message_type;
+
+    /*
+     * For this copy to the guest ring, tx_ptr is always 16-byte aligned
+     * and the message header is 16 bytes long.
+     */
+    BUILD_BUG_ON(
+        sizeof(struct xen_argo_ring_message_header) != ROUNDUP_MESSAGE(1));
+
+    /*
+     * First data write into the destination ring: fixed size, message header.
+     * This cannot overrun because the available free space (value in 'sp')
+     * is checked above and must be at least this size.
+     */
+    ret = memcpy_to_guest_ring(ring_info, ring.tx_ptr + sizeof(xen_argo_ring_t),
+                               &mh, NULL_hnd, sizeof(mh));
+    if ( ret )
+    {
+        gprintk(XENLOG_ERR,
+                "argo: failed to write message header to ring (vm%u:%x vm%d)\n",
+                ring_info->id.domain_id, ring_info->id.port,
+                ring_info->id.partner_id);
+

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 636

Quoted 10 lines
xen/common/argo.c +10 -0
+        goto out;
+    }
+
+    ring.tx_ptr += sizeof(mh);
+    if ( ring.tx_ptr == ring_info->len )
+        ring.tx_ptr = 0;
+
+    piov = iovs;
+
+    while ( niov-- )

AFAICT using a for loop would remove the need to also do a piov++ at each iteration.

Quoted 104 lines
xen/common/argo.c +104 -0
+    {
+        XEN_GUEST_HANDLE_64(uint8_t) buf_hnd = piov->iov_hnd;
+        uint32_t iov_len = piov->iov_len;
+
+        /* If no data is provided in this iov, moan and skip on to the next */
+        if ( !iov_len )
+        {
+            gprintk(XENLOG_ERR,
+                    "argo: no data iov_len=0 iov_hnd=%p ring (vm%u:%x vm%d)\n",
+                    buf_hnd.p, ring_info->id.domain_id, ring_info->id.port,
+                    ring_info->id.partner_id);
+
+            piov++;
+            continue;
+        }
+
+        if ( unlikely(!guest_handle_okay(buf_hnd, iov_len)) )
+        {
+            gprintk(XENLOG_ERR,
+                    "argo: bad iov handle [%p, %"PRIx32"] (vm%u:%x vm%d)\n",
+                    buf_hnd.p, iov_len,
+                    ring_info->id.domain_id, ring_info->id.port,
+                    ring_info->id.partner_id);
+
+            ret = -EFAULT;
+            goto out;
+        }
+
+        sp = ring_info->len - ring.tx_ptr;
+
+        /* Check: iov data size versus free space at the tail of the ring */
+        if ( iov_len > sp )
+        {
+            /*
+             * Second possible data write: ring-tail-wrap-write.
+             * Populate the ring tail and update the internal tx_ptr to handle
+             * wrapping at the end of ring.
+             * Size of data written here: sp
+             * which is the exact full amount of free space available at the
+             * tail of the ring, so this cannot overrun.
+             */
+            ret = memcpy_to_guest_ring(ring_info,
+                                       ring.tx_ptr + sizeof(xen_argo_ring_t),
+                                       NULL, buf_hnd, sp);
+            if ( ret )
+            {
+                gprintk(XENLOG_ERR,
+                        "argo: failed to copy {%p, %"PRIx32"} (vm%u:%x vm%d)\n",
+                        buf_hnd.p, sp,
+                        ring_info->id.domain_id, ring_info->id.port,
+                        ring_info->id.partner_id);
+
+                goto out;
+            }
+
+            ring.tx_ptr = 0;
+            iov_len -= sp;
+            guest_handle_add_offset(buf_hnd, sp);
+
+            ASSERT(iov_len <= ring_info->len);
+        }
+
+        /*
+         * Third possible data write: all data remaining for this iov.
+         * Size of data written here: iov_len
+         *
+         * Case 1: if the ring-tail-wrap-write above was performed, then
+         *         iov_len has been decreased by 'sp' and ring.tx_ptr is zero.
+         *
+         *    We know from checking the result of iov_count:
+         *      len + sizeof(message_header) <= ring_info->len
+         *    We also know that len is the total of summing all iov_lens, so:
+         *       iov_len <= len
+         *    so by transitivity:
+         *       iov_len <= len <= (ring_info->len - sizeof(msgheader))
+         *    and therefore:
+         *       (iov_len + sizeof(msgheader) <= ring_info->len) &&
+         *       (ring.tx_ptr == 0)
+         *    so this write cannot overrun here.
+         *
+         * Case 2: ring-tail-wrap-write above was not performed
+         *    -> so iov_len is the guest-supplied value and: (iov_len <= sp)
+         *    ie. less than available space at the tail of the ring:
+         *        so this write cannot overrun.
+         */
+        ret = memcpy_to_guest_ring(ring_info,
+                                   ring.tx_ptr + sizeof(xen_argo_ring_t),
+                                   NULL, buf_hnd, iov_len);
+        if ( ret )
+        {
+            gprintk(XENLOG_ERR,
+                    "argo: failed to copy [%p, %"PRIx32"] (vm%u:%x vm%d)\n",
+                    buf_hnd.p, iov_len, ring_info->id.domain_id,
+                    ring_info->id.port, ring_info->id.partner_id);
+
+            goto out;
+        }
+
+        ring.tx_ptr += iov_len;
+
+        if ( ring.tx_ptr == ring_info->len )
+            ring.tx_ptr = 0;
+
+        piov++;

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 750

Quoted 10 lines
xen/common/argo.c +10 -0
+    }
+
+    ring.tx_ptr = ROUNDUP_MESSAGE(ring.tx_ptr);
+
+    if ( ring.tx_ptr >= ring_info->len )
+        ring.tx_ptr -= ring_info->len;
+
+    update_tx_ptr(ring_info, ring.tx_ptr);
+
+ out:

Do you really need to out label? *out_len it's only set in the success case, so all the error cases that use a 'goto out' could be replaced by 'return ret;'.

Quoted 49 lines
xen/common/argo.c +35 -0
+    /*
+     * At this point it is possible to unmap the ring_info, ie:
+     *   ring_unmap(ring_info);
+     * but performance should be improved by not doing so, and retaining
+     * the mapping.
+     * An XSM policy control over level of confidentiality required
+     * versus performance cost could be added to decide that here.
+     * See the similar comment in ring_map_page re: write-only mappings.
+     */
+
+    if ( !ret )
+        *out_len = len;
+
+    return ret;
+}
+
 static void
 wildcard_pending_list_remove(domid_t domain_id, struct pending_ent *ent)
 {
@@ -359,6 +781,22 @@ wildcard_pending_list_remove(domid_t domain_id, struct pending_ent *ent)
 }
 
 static void
+wildcard_pending_list_insert(domid_t domain_id, struct pending_ent *ent)
+{
+    struct domain *d = get_domain_by_id(domain_id);
+    if ( !d )
+        return;
+
+    if ( d->argo )
+    {
+        spin_lock(&d->argo->wildcard_lock);
+        hlist_add_head(&ent->wildcard_node, &d->argo->wildcard_pend_list);
+        spin_unlock(&d->argo->wildcard_lock);
+    }
+    put_domain(d);
+}
+
+static void
 pending_remove_all(struct argo_ring_info *ring_info)
 {
     struct hlist_node *node, *next;
@@ -374,6 +812,67 @@ pending_remove_all(struct argo_ring_info *ring_info)
     ring_info->npending = 0;
 }
 
+static int
+pending_queue(struct argo_ring_info *ring_info, domid_t src_id,
+              unsigned int len)

xen/common/argo.c pending_remove_all(struct argo_ring_info *ring_info) · line 827

Quoted 10 lines
xen/common/argo.c +10 -0
+{
+    struct pending_ent *ent;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    if ( ring_info->npending >= MAX_PENDING_PER_RING )
+        return -ENOSPC;
+
+    ent = xmalloc(struct pending_ent);
+

Extra newline.

Quoted 118 lines
xen/common/argo.c +104 -0
+    if ( !ent )
+        return -ENOMEM;
+
+    ent->len = len;
+    ent->domain_id = src_id;
+    ent->ring_info = ring_info;
+
+    if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+        wildcard_pending_list_insert(src_id, ent);
+    hlist_add_head(&ent->node, &ring_info->pending);
+    ring_info->npending++;
+
+    return 0;
+}
+
+static int
+pending_requeue(struct argo_ring_info *ring_info, domid_t src_id,
+                unsigned int len)
+{
+    struct hlist_node *node;
+    struct pending_ent *ent;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    hlist_for_each_entry(ent, node, &ring_info->pending, node)
+    {
+        if ( ent->domain_id == src_id )
+        {
+            /*
+             * Reuse an existing queue entry for a notification rather than add
+             * another. If the existing entry is waiting for a smaller size than
+             * the current message then adjust the record to wait for the
+             * current (larger) size to be available before triggering a
+             * notification.
+             * This assists the waiting sender by ensuring that whenever a
+             * notification is triggered, there is sufficient space available
+             * for (at least) any one of the messages awaiting transmission.
+             */
+            if ( ent->len < len )
+                ent->len = len;
+
+            return 0;
+        }
+    }
+
+    return pending_queue(ring_info, src_id, len);
+}
+
 static void
 wildcard_rings_pending_remove(struct domain *d)
 {
@@ -667,6 +1166,28 @@ ring_find_info(const struct domain *d, const struct argo_ring_id *id)
     return NULL;
 }
 
+static struct argo_ring_info *
+ring_find_info_by_match(const struct domain *d, uint32_t port,
+                        domid_t partner_id)
+{
+    struct argo_ring_id id;
+    struct argo_ring_info *ring_info;
+
+    ASSERT(rw_is_locked(&d->argo->lock));
+
+    id.port = port;
+    id.domain_id = d->domain_id;
+    id.partner_id = partner_id;
+
+    ring_info = ring_find_info(d, &id);
+    if ( ring_info )
+        return ring_info;
+
+    id.partner_id = XEN_ARGO_DOMID_ANY;
+
+    return ring_find_info(d, &id);
+}
+
 static struct argo_send_info *
 send_find_info(const struct domain *d, const struct argo_ring_id *id)
 {
@@ -1005,6 +1526,95 @@ register_ring(struct domain *currd,
     return ret;
 }
 
+static long
+sendv(struct domain *src_d, const xen_argo_addr_t *src_addr,
+      const xen_argo_addr_t *dst_addr,
+      XEN_GUEST_HANDLE_PARAM(xen_argo_iov_t) iovs_hnd, unsigned long niov,
+      uint32_t message_type)
+{
+    struct domain *dst_d = NULL;
+    struct argo_ring_id src_id;
+    struct argo_ring_info *ring_info;
+    int ret = 0;
+    unsigned long len = 0;
+
+    ASSERT(src_d->domain_id == src_addr->domain_id);
+
+    argo_dprintk("sendv: (%d:%x)->(%d:%x) niov:%lu iov:%p type:%u\n",
+                 src_addr->domain_id, src_addr->port,
+                 dst_addr->domain_id, dst_addr->port,
+                 niov, iovs_hnd.p, message_type);
+
+    read_lock(&argo_lock);
+
+    if ( !src_d->argo )
+    {
+        ret = -ENODEV;
+        goto out_unlock;
+    }
+
+    src_id.port = src_addr->port;
+    src_id.domain_id = src_d->domain_id;
+    src_id.partner_id = dst_addr->domain_id;
+
+    dst_d = get_domain_by_id(dst_addr->domain_id);
+    if ( !dst_d )
+    {

xen/common/argo.c register_ring(struct domain *currd, · line 1572

Quoted 10 lines
xen/common/argo.c +10 -0
+        argo_dprintk("!dst_d, ESRCH\n");
+        ret = -ESRCH;
+        goto out_unlock;
+    }
+
+    if ( !dst_d->argo )
+    {
+        argo_dprintk("!dst_d->argo, ECONNREFUSED\n");
+        ret = -ECONNREFUSED;
+        goto out_unlock;

The usage of out_unlock here and in the condition above is wrong, since it will unconditionally call read_unlock(&argo_lock); which is wrong here because the lock has not yet been acquired.

Quoted 24 lines
xen/common/argo.c +24 -0
+    }
+
+    read_lock(&dst_d->argo->lock);
+
+    ring_info = ring_find_info_by_match(dst_d, dst_addr->port,
+                                        src_addr->domain_id);
+    if ( !ring_info )
+    {
+        gprintk(XENLOG_ERR,
+                "argo: vm%u connection refused, src (vm%u:%x) dst (vm%u:%x)\n",
+                current->domain->domain_id, src_id.domain_id, src_id.port,
+                dst_addr->domain_id, dst_addr->port);
+
+        ret = -ECONNREFUSED;
+        goto out_unlock2;
+    }
+
+    spin_lock(&ring_info->lock);
+
+    ret = ringbuf_insert(dst_d, ring_info, &src_id, iovs_hnd, niov,
+                         message_type, &len);
+    if ( ret == -EAGAIN )
+    {
+        argo_dprintk("argo_ringbuf_sendv failed, EAGAIN\n");

xen/common/argo.c register_ring(struct domain *currd, · line 1606

Quoted 10 lines
xen/common/argo.c +10 -0
+        /* requeue to issue a notification when space is there */
+        ret = pending_requeue(ring_info, src_addr->domain_id, len);
+    }
+
+    spin_unlock(&ring_info->lock);
+
+    if ( ret >= 0 )
+        signal_domain(dst_d);
+
+ out_unlock2:

There's only a single user of the out_unlock2 label, at which point it might be easier to read to just put the read_unlock there and just use the existing out_unlock label.

Thanks, Roger.

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

#79 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Thanks for the review, Roger. Replies inline below.

Quoted 29 lines · Roger Pau Monné

to.On Mon, Jan 7, 2019 at 8:44 AM Christopher Clark

Quoted 26 lines · <[email protected]> wrote:

sendv operation is invoked to perform a synchronous send of buffers contained in iovs to a remote domain's registered ring.

xen/common/argo.c +16 -0
index 59ce8c4..4548435 100644
 
 
+static int
+memcpy_to_guest_ring(struct argo_ring_info *ring_info, uint32_t offset,
+                     const void *src, XEN_GUEST_HANDLE(uint8_t) src_hnd,
+                     uint32_t len)
+{
+    unsigned int mfns_index = offset >> PAGE_SHIFT;
+    void *dst;
+    int ret;
+    unsigned int src_offset = 0;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    offset &= ~PAGE_MASK;
+
+    if ( (len > XEN_ARGO_MAX_RING_SIZE) || (offset > XEN_ARGO_MAX_RING_SIZE) )
+        return -EFAULT;
Quoted 8 lines

xen/common/argo.c

Quoted 2 lines
xen/common/argo.c +2 -0
+
+    while ( (offset + len) > PAGE_SIZE )

I think you could map the whole ring in contiguous virtual address space and then writing to it would be much more easy, you wouldn't need to iterate with memcpy or copy_from_guest, take a look at __vmap. You could likely map this when the ring gets setup and keep it mapped for the lifetime of the ring.

You're right about that, because map_domain_page_global, which the current code uses, uses vmap itself. I think there's a couple of reasons why the code has been implemented the iterative way though.

The first is that I think ring resize has been a consideration: it's useful to be able to increase the size of a live and active ring that is under load without having to tear down the mappings, find a new virtual address region of the right size and then remap it: you can just supply some more memory and map those pages onto the end of the ring, and ensure both sides know about the new ring size. Similarly, shrinking a quiet ring can be useful.
However, the "gfn race" that you (correctly) pointed out in an earlier review, and Jan's related request to drop the "revalidate an existing mapping on ring reregister" motivated removal of a section of the code involved, and then in v3 of the series, I've actually just blocked ring resize because it's missing a walk through the pending notifications to find any that have become untriggerable with the new ring size when a ring is shrunk and I'd like to defer implementing that for now. So the ring resize reason is more of a consideration for a possible later version of Argo than the current one.

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB). With single-page allocations to build a ring, fragmentation is not a problem, and mischief by a guest seems difficult. Changing it to issue requests for contiguous regions, with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

Quoted 22 lines
Quoted 9 lines
xen/common/argo.c +8 -0
+    {
+        unsigned int head_len = PAGE_SIZE - offset;
+
+        ret = ring_map_page(ring_info, mfns_index, &dst);
+        if ( ret )
+            return ret;
+
+        if ( src )

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 395

Quoted 10 lines
xen/common/argo.c +10 -0
+        {
+            memcpy(dst + offset, src + src_offset, head_len);
+            src_offset += head_len;
+        }
+        else
+        {
+            ret = copy_from_guest(dst + offset, src_hnd, head_len) ?
+                    -EFAULT : 0;
+            if ( ret )
+                return ret;

You can simplify this to:

Quoted 2 lines

if ( copy_from_guest(...) )

    return -EFAULT;

yes! ack - thanks

<snip>

Quoted 17 lines
Quoted 17 lines
xen/common/argo.c +17 -0
+/*
+ * get_sanitized_ring creates a modified copy of the ring pointers where
+ * the rx_ptr is rounded up to ensure it is aligned, and then ring
+ * wrap is handled. Simplifies safe use of the rx_ptr for available
+ * space calculation.
+ */
+static int
+get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info)
+{
+    uint32_t rx_ptr;
+    int ret;
+
+    ret = get_rx_ptr(ring_info, &rx_ptr);
+    if ( ret )
+        return ret;
+
+    ring->tx_ptr = ring_info->tx_ptr;
Quoted 8 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 472

Quoted 6 lines
xen/common/argo.c +6 -0
+
+    rx_ptr = ROUNDUP_MESSAGE(rx_ptr);
+    if ( rx_ptr >= ring_info->len )
+        rx_ptr = 0;
+
+    ring->rx_ptr = rx_ptr;

Newline.

ack, thanks

<snip>

Quoted 18 lines
Quoted 4 lines
xen/common/argo.c +4 -0
+/*
+ * iov_count returns its count on success via an out variable to avoid
+ * potential for a negative return value to be used incorrectly
+ * (eg. coerced into an unsigned variable resulting in a large incorrect value)

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 489

Quoted 10 lines
xen/common/argo.c +10 -0
+ */
+static int
+iov_count(const xen_argo_iov_t *piov, unsigned long niov, uint32_t *count)
+{
+    uint32_t sum_iov_lens = 0;
+
+    if ( niov > XEN_ARGO_MAXIOV )
+        return -EINVAL;
+
+    while ( niov-- )

I would use a for loop here, that would remove the need to piov++, if you want to keep it quite similar:

Quoted 2 lines

for ( ; niov--; piov++ )
{

Yes, that is better - thanks, applied.

<snip>

Quoted 4 lines
Quoted 4 lines
xen/common/argo.c +4 -0
+
+static int
+ringbuf_insert(struct domain *d, struct argo_ring_info *ring_info,
+               const struct argo_ring_id *src_id,
Quoted 8 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 534

Quoted 6 lines
xen/common/argo.c +6 -0
+               XEN_GUEST_HANDLE_PARAM(xen_argo_iov_t) iovs_hnd,
+               unsigned long niov, uint32_t message_type,
+               unsigned long *out_len)
+{
+    xen_argo_ring_t ring;
+    struct xen_argo_ring_message_header mh = { 0 };

No need for the 0, { } will achieve exactly the same.

ack, applied

Quoted 10 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 538

Quoted 5 lines
xen/common/argo.c +4 -0
+    int32_t sp;
+    int32_t ret;
+    uint32_t len = 0;
+    xen_argo_iov_t iovs[XEN_ARGO_MAXIOV];

This seems slightly dangerous, a change of the maximum could cause stack overflow depending on the size of xen_argo_iov_t. I think you need some comment next to definition of XEN_ARGO_MAXIOV to note that increasing this could cause issues.

That makes sense, will do.

<snip>

Quoted 19 lines
Quoted 19 lines
xen/common/argo.c +19 -0
+    /*
+     * First data write into the destination ring: fixed size, message header.
+     * This cannot overrun because the available free space (value in 'sp')
+     * is checked above and must be at least this size.
+     */
+    ret = memcpy_to_guest_ring(ring_info, ring.tx_ptr + sizeof(xen_argo_ring_t),
+                               &mh, NULL_hnd, sizeof(mh));
+    if ( ret )
+    {
+        gprintk(XENLOG_ERR,
+                "argo: failed to write message header to ring (vm%u:%x vm%d)\n",
+                ring_info->id.domain_id, ring_info->id.port,
+                ring_info->id.partner_id);
+
+        goto out;
+    }
+
+    ring.tx_ptr += sizeof(mh);
+    if ( ring.tx_ptr == ring_info->len )
Quoted 8 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 636

Quoted 5 lines
xen/common/argo.c +5 -0
+        ring.tx_ptr = 0;
+
+    piov = iovs;
+
+    while ( niov-- )

AFAICT using a for loop would remove the need to also do a piov++ at each iteration.

ack, applied.

<snip>

Quoted 30 lines
Quoted 30 lines
xen/common/argo.c +30 -0
+         * Case 2: ring-tail-wrap-write above was not performed
+         *    -> so iov_len is the guest-supplied value and: (iov_len <= sp)
+         *    ie. less than available space at the tail of the ring:
+         *        so this write cannot overrun.
+         */
+        ret = memcpy_to_guest_ring(ring_info,
+                                   ring.tx_ptr + sizeof(xen_argo_ring_t),
+                                   NULL, buf_hnd, iov_len);
+        if ( ret )
+        {
+            gprintk(XENLOG_ERR,
+                    "argo: failed to copy [%p, %"PRIx32"] (vm%u:%x vm%d)\n",
+                    buf_hnd.p, iov_len, ring_info->id.domain_id,
+                    ring_info->id.port, ring_info->id.partner_id);
+
+            goto out;
+        }
+
+        ring.tx_ptr += iov_len;
+
+        if ( ring.tx_ptr == ring_info->len )
+            ring.tx_ptr = 0;
+
+        piov++;
+    }
+
+    ring.tx_ptr = ROUNDUP_MESSAGE(ring.tx_ptr);
+
+    if ( ring.tx_ptr >= ring_info->len )
+        ring.tx_ptr -= ring_info->len;
Quoted 8 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 750

Quoted 4 lines
xen/common/argo.c +4 -0
+
+    update_tx_ptr(ring_info, ring.tx_ptr);
+
+ out:

Do you really need to out label? *out_len it's only set in the success case, so all the error cases that use a 'goto out' could be replaced by 'return ret;'.

ack, thanks -- done.

<snip>

Quoted 7 lines
Quoted 7 lines
xen/common/argo.c +7 -0
+static int
+pending_queue(struct argo_ring_info *ring_info, domid_t src_id,
+              unsigned int len)
+{
+    struct pending_ent *ent;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
Quoted 8 lines

xen/common/argo.c pending_remove_all(struct argo_ring_info *ring_info) · line 827

Quoted 6 lines
xen/common/argo.c +6 -0
+
+    if ( ring_info->npending >= MAX_PENDING_PER_RING )
+        return -ENOSPC;
+
+    ent = xmalloc(struct pending_ent);
+

Extra newline.

ack

<snip>

Quoted 41 lines
Quoted 41 lines
xen/common/argo.c +40 -0
+static long
+sendv(struct domain *src_d, const xen_argo_addr_t *src_addr,
+      const xen_argo_addr_t *dst_addr,
+      XEN_GUEST_HANDLE_PARAM(xen_argo_iov_t) iovs_hnd, unsigned long niov,
+      uint32_t message_type)
+{
+    struct domain *dst_d = NULL;
+    struct argo_ring_id src_id;
+    struct argo_ring_info *ring_info;
+    int ret = 0;
+    unsigned long len = 0;
+
+    ASSERT(src_d->domain_id == src_addr->domain_id);
+
+    argo_dprintk("sendv: (%d:%x)->(%d:%x) niov:%lu iov:%p type:%u\n",
+                 src_addr->domain_id, src_addr->port,
+                 dst_addr->domain_id, dst_addr->port,
+                 niov, iovs_hnd.p, message_type);
+
+    read_lock(&argo_lock);
+
+    if ( !src_d->argo )
+    {
+        ret = -ENODEV;
+        goto out_unlock;
+    }
+
+    src_id.port = src_addr->port;
+    src_id.domain_id = src_d->domain_id;
+    src_id.partner_id = dst_addr->domain_id;
+
+    dst_d = get_domain_by_id(dst_addr->domain_id);
+    if ( !dst_d )
+    {
+        argo_dprintk("!dst_d, ESRCH\n");
+        ret = -ESRCH;
+        goto out_unlock;
+    }
+
+    if ( !dst_d->argo )
Quoted 8 lines

xen/common/argo.c register_ring(struct domain *currd, · line 1572

Quoted 4 lines
xen/common/argo.c +4 -0
+    {
+        argo_dprintk("!dst_d->argo, ECONNREFUSED\n");
+        ret = -ECONNREFUSED;
+        goto out_unlock;

The usage of out_unlock here and in the condition above is wrong, since it will unconditionally call read_unlock(&argo_lock); which is wrong here because the lock has not yet been acquired.

Sorry, I don't think that's quite right -- if you scroll up a bit here, you can see where argo_lock is taken unconditionally, just after the dprintk and before checking whether src_d is argo enabled. The second lock hasn't been taken yet - but that's not the one being unlocked on that out_unlock path.

Quoted 31 lines
Quoted 31 lines
xen/common/argo.c +30 -0
+    }
+
+    read_lock(&dst_d->argo->lock);
+
+    ring_info = ring_find_info_by_match(dst_d, dst_addr->port,
+                                        src_addr->domain_id);
+    if ( !ring_info )
+    {
+        gprintk(XENLOG_ERR,
+                "argo: vm%u connection refused, src (vm%u:%x) dst (vm%u:%x)\n",
+                current->domain->domain_id, src_id.domain_id, src_id.port,
+                dst_addr->domain_id, dst_addr->port);
+
+        ret = -ECONNREFUSED;
+        goto out_unlock2;
+    }
+
+    spin_lock(&ring_info->lock);
+
+    ret = ringbuf_insert(dst_d, ring_info, &src_id, iovs_hnd, niov,
+                         message_type, &len);
+    if ( ret == -EAGAIN )
+    {
+        argo_dprintk("argo_ringbuf_sendv failed, EAGAIN\n");
+        /* requeue to issue a notification when space is there */
+        ret = pending_requeue(ring_info, src_addr->domain_id, len);
+    }
+
+    spin_unlock(&ring_info->lock);
+
Quoted 8 lines

xen/common/argo.c register_ring(struct domain *currd, · line 1606

Quoted 4 lines
xen/common/argo.c +4 -0
+    if ( ret >= 0 )
+        signal_domain(dst_d);
+
+ out_unlock2:

There's only a single user of the out_unlock2 label, at which point it might be easier to read to just put the read_unlock there and just use the existing out_unlock label.

ack, will change that.

Thanks again,

Christopher

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

#80 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 46 lines · Christopher Clark

Thanks for the review, Roger. Replies inline below.

Quoted 29 lines · Roger Pau Monné

to.On Mon, Jan 7, 2019 at 8:44 AM Christopher Clark

Quoted 26 lines · <[email protected]> wrote:

sendv operation is invoked to perform a synchronous send of buffers contained in iovs to a remote domain's registered ring.

xen/common/argo.c +16 -0
index 59ce8c4..4548435 100644
 
 
+static int
+memcpy_to_guest_ring(struct argo_ring_info *ring_info, uint32_t offset,
+                     const void *src, XEN_GUEST_HANDLE(uint8_t) src_hnd,
+                     uint32_t len)
+{
+    unsigned int mfns_index = offset >> PAGE_SHIFT;
+    void *dst;
+    int ret;
+    unsigned int src_offset = 0;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    offset &= ~PAGE_MASK;
+
+    if ( (len > XEN_ARGO_MAX_RING_SIZE) || (offset > XEN_ARGO_MAX_RING_SIZE) )
+        return -EFAULT;
Quoted 8 lines

xen/common/argo.c

Quoted 2 lines
xen/common/argo.c +2 -0
+
+    while ( (offset + len) > PAGE_SIZE )

I think you could map the whole ring in contiguous virtual address space and then writing to it would be much more easy, you wouldn't need to iterate with memcpy or copy_from_guest, take a look at __vmap. You could likely map this when the ring gets setup and keep it mapped for the lifetime of the ring.

You're right about that, because map_domain_page_global, which the current code uses, uses vmap itself. I think there's a couple of reasons why the code has been implemented the iterative way though.

Quoted 7 lines

The first is that I think ring resize has been a consideration: it's useful to be able to increase the size of a live and active ring that is under load without having to tear down the mappings, find a new virtual address region of the right size and then remap it: you can just supply some more memory and map those pages onto the end of the ring, and ensure both sides know about the new ring size. Similarly, shrinking a quiet ring can be useful.

Is such on the fly expansion something common with argo?

I'm not saying it's something that shouldn't be supported, but the burden of allowing such resizing doesn't seem trivial. You will have to redimension a lot of the arrays used to store the pages used, and at that point I wonder whether remapping the virtual address space is really the biggest issue you are going to have if you allow such run time resizing.

Quoted 10 lines

However, the "gfn race" that you (correctly) pointed out in an earlier review, and Jan's related request to drop the "revalidate an existing mapping on ring reregister" motivated removal of a section of the code involved, and then in v3 of the series, I've actually just blocked ring resize because it's missing a walk through the pending notifications to find any that have become untriggerable with the new ring size when a ring is shrunk and I'd like to defer implementing that for now. So the ring resize reason is more of a consideration for a possible later version of Argo than the current one.

Quoted 3 lines

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB).

As said in another reply, I'm not sure allowing 16GB rings is safe. The amount of internal memory required to track such rings is not trivial given the arrays to store the mfns, the pages, and the virtual mappings.

Quoted 3 lines

With single-page allocations to
build a ring, fragmentation is not a problem, and mischief by a guest seems difficult.

Hm, there's still a lot of big dynamic memory allocations in order to support a 16GB ring, which makes me think that virtual address space is not the only problem if you allow 16GB rings.

Quoted 8 lines

Changing it to issue requests for contiguous regions,
with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

I haven't checked, but I would be quite surprised to find any vmap usage with such size (16GB). Maybe someone more familiar with the mm subsystem can provide some insight here.

Quoted 6 lines

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

My opinion would be to just use vmap if it works, because that IMO greatly simplifies the code by being able to have the whole ring mapped at all the time. It would remove the iteration to copy requests, and remove the usage of ring_map_page everywhere. That would be my recommendation code-wise, but as said above someone more familiar with the mm subsystem might have other opinion's about how to deal with accesses to 16GB of guest memory, and indeed your iterative solution might be the best approach.

Quoted 49 lines
Quoted 40 lines
Quoted 40 lines
xen/common/argo.c +40 -0
+static long
+sendv(struct domain *src_d, const xen_argo_addr_t *src_addr,
+      const xen_argo_addr_t *dst_addr,
+      XEN_GUEST_HANDLE_PARAM(xen_argo_iov_t) iovs_hnd, unsigned long niov,
+      uint32_t message_type)
+{
+    struct domain *dst_d = NULL;
+    struct argo_ring_id src_id;
+    struct argo_ring_info *ring_info;
+    int ret = 0;
+    unsigned long len = 0;
+
+    ASSERT(src_d->domain_id == src_addr->domain_id);
+
+    argo_dprintk("sendv: (%d:%x)->(%d:%x) niov:%lu iov:%p type:%u\n",
+                 src_addr->domain_id, src_addr->port,
+                 dst_addr->domain_id, dst_addr->port,
+                 niov, iovs_hnd.p, message_type);
+
+    read_lock(&argo_lock);
+
+    if ( !src_d->argo )
+    {
+        ret = -ENODEV;
+        goto out_unlock;
+    }
+
+    src_id.port = src_addr->port;
+    src_id.domain_id = src_d->domain_id;
+    src_id.partner_id = dst_addr->domain_id;
+
+    dst_d = get_domain_by_id(dst_addr->domain_id);
+    if ( !dst_d )
+    {
+        argo_dprintk("!dst_d, ESRCH\n");
+        ret = -ESRCH;
+        goto out_unlock;
+    }
+
+    if ( !dst_d->argo )
Quoted 8 lines

xen/common/argo.c register_ring(struct domain *currd, · line 1572

Quoted 4 lines
xen/common/argo.c +4 -0
+    {
+        argo_dprintk("!dst_d->argo, ECONNREFUSED\n");
+        ret = -ECONNREFUSED;
+        goto out_unlock;

The usage of out_unlock here and in the condition above is wrong, since it will unconditionally call read_unlock(&argo_lock); which is wrong here because the lock has not yet been acquired.

Quoted 5 lines

Sorry, I don't think that's quite right -- if you scroll up a bit here, you can see where argo_lock is taken unconditionally, just after the dprintk and before checking whether src_d is argo enabled. The second lock hasn't been taken yet - but that's not the one being unlocked on that out_unlock path.

Ops, yes, sorry. Got messed up with so many locks.

Thanks, Roger.

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

#81 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 10.01.19 at 13:01, <[email protected]> wrote:
Quoted 4 lines · Christopher Clark

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB).

As said in another reply, I'm not sure allowing 16GB rings is safe. The amount of internal memory required to track such rings is not trivial given the arrays to store the mfns, the pages, and the virtual mappings.

Quoted 3 lines

With single-page allocations to
build a ring, fragmentation is not a problem, and mischief by a guest seems difficult.

Hm, there's still a lot of big dynamic memory allocations in order to support a 16GB ring, which makes me think that virtual address space is not the only problem if you allow 16GB rings.

Quoted 8 lines

Changing it to issue requests for contiguous regions,
with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

Quoted 3 lines

I haven't checked, but I would be quite surprised to find any vmap usage with such size (16GB). Maybe someone more familiar with the mm subsystem can provide some insight here.

And indeed the vmap range reserved in VA space is just 64GB (on x86) at present.

Quoted 7 lines
Quoted 6 lines

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

Quoted 8 lines

My opinion would be to just use vmap if it works, because that IMO greatly simplifies the code by being able to have the whole ring mapped at all the time. It would remove the iteration to copy requests, and remove the usage of ring_map_page everywhere. That would be my recommendation code-wise, but as said above someone more familiar with the mm subsystem might have other opinion's about how to deal with accesses to 16GB of guest memory, and indeed your iterative solution might be the best approach.

No-one can allocate 16GB physically contiguous memory.

Jan

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

#82 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 53 lines · Jan Beulich
Quoted 30 lines
Quoted 2 lines
Quoted 2 lines · On 10.01.19 at 13:01, <[email protected]> wrote:
Quoted 4 lines · Christopher Clark

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB).

As said in another reply, I'm not sure allowing 16GB rings is safe. The amount of internal memory required to track such rings is not trivial given the arrays to store the mfns, the pages, and the virtual mappings.

Quoted 3 lines

With single-page allocations to
build a ring, fragmentation is not a problem, and mischief by a guest seems difficult.

Hm, there's still a lot of big dynamic memory allocations in order to support a 16GB ring, which makes me think that virtual address space is not the only problem if you allow 16GB rings.

Quoted 8 lines

Changing it to issue requests for contiguous regions,
with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

Quoted 3 lines

I haven't checked, but I would be quite surprised to find any vmap usage with such size (16GB). Maybe someone more familiar with the mm subsystem can provide some insight here.

And indeed the vmap range reserved in VA space is just 64GB (on x86) at present.

Quoted 7 lines
Quoted 6 lines

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

Quoted 8 lines

My opinion would be to just use vmap if it works, because that IMO greatly simplifies the code by being able to have the whole ring mapped at all the time. It would remove the iteration to copy requests, and remove the usage of ring_map_page everywhere. That would be my recommendation code-wise, but as said above someone more familiar with the mm subsystem might have other opinion's about how to deal with accesses to 16GB of guest memory, and indeed your iterative solution might be the best approach.

Quoted 1 line

No-one can allocate 16GB physically contiguous memory.

Right, my question/comment was whether it would make sense to limit the size of the argos ring to something smaller and then use vmap to map the whole ring in contiguous virtual space in order to ease accesses.

TBH, I'm not sure virtual address space is the only issue if argos allows 16GB rings to be used. 16GB rings will consume a non-trivial amount of memory for the internal argos state tracking structures AFAICT.

Thanks, Roger.

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

#83 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 56 lines
Quoted 1 line
Quoted 1 line · On 10.01.19 at 13:40, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines · On 10.01.19 at 13:01, <[email protected]> wrote:

On Thu, Jan 10, 2019 at 4:10 AM Christopher Clark

Quoted 50 lines · <[email protected]> wrote:
Quoted 27 lines
Quoted 4 lines

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB).

As said in another reply, I'm not sure allowing 16GB rings is safe. The amount of internal memory required to track such rings is not trivial given the arrays to store the mfns, the pages, and the virtual mappings.

Quoted 3 lines

With single-page allocations to
build a ring, fragmentation is not a problem, and mischief by a guest seems difficult.

Hm, there's still a lot of big dynamic memory allocations in order to support a 16GB ring, which makes me think that virtual address space is not the only problem if you allow 16GB rings.

Quoted 8 lines

Changing it to issue requests for contiguous regions,
with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

Quoted 3 lines

I haven't checked, but I would be quite surprised to find any vmap usage with such size (16GB). Maybe someone more familiar with the mm subsystem can provide some insight here.

And indeed the vmap range reserved in VA space is just 64GB (on x86) at present.

Quoted 15 lines
Quoted 6 lines

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

My opinion would be to just use vmap if it works, because that IMO greatly simplifies the code by being able to have the whole ring mapped at all the time. It would remove the iteration to copy requests, and remove the usage of ring_map_page everywhere. That would be my recommendation code-wise, but as said above someone more familiar with the mm subsystem might have other opinion's about how to deal with accesses to 16GB of guest memory, and indeed your iterative solution might be the best approach.

Quoted 6 lines
Quoted 1 line

No-one can allocate 16GB physically contiguous memory.

Right, my question/comment was whether it would make sense to limit the size of the argos ring to something smaller and then use vmap to map the whole ring in contiguous virtual space in order to ease accesses.

Whether you vmap() the ring in (page sized) pieces or in one blob is, for the purpose of the order of magnitude of VA space consumption, not overly relevant: You can't map more than at most three such gigantic rings anyway with the current VA layout. (In practice mapping individual pages would halve the effectively usable VA space, due to the guard pages inserted between regions.) IOW - the ring size should be bounded at a lower value anyway imo.

Quoted 4 lines

TBH, I'm not sure virtual address space is the only issue if argos allows 16GB rings to be used. 16GB rings will consume a non-trivial amount of memory for the internal argos state tracking structures AFAICT.

Fully agree. It has taken us ages to eliminate all runtime allocations of order > 0, and it looks like we'd be gaining some back here. I consider this tolerable as long as the feature is experimental, but it would need fixing for it to become fully supported. Christopher - annotating such code with fixme comments right away helps later spotting (and addressing) them.

Jan

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

#84 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 77 lines · Jan Beulich
Quoted 57 lines
Quoted 2 lines
Quoted 2 lines · On 10.01.19 at 13:40, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines · On 10.01.19 at 13:01, <[email protected]> wrote:

On Thu, Jan 10, 2019 at 4:10 AM Christopher Clark

Quoted 50 lines · <[email protected]> wrote:
Quoted 27 lines
Quoted 4 lines

The second reason is about avoiding exposing the Xen virtual memory allocator directly to frequent guest-supplied size requests for contiguous regions (of up to 16GB).

As said in another reply, I'm not sure allowing 16GB rings is safe. The amount of internal memory required to track such rings is not trivial given the arrays to store the mfns, the pages, and the virtual mappings.

Quoted 3 lines

With single-page allocations to
build a ring, fragmentation is not a problem, and mischief by a guest seems difficult.

Hm, there's still a lot of big dynamic memory allocations in order to support a 16GB ring, which makes me think that virtual address space is not the only problem if you allow 16GB rings.

Quoted 8 lines

Changing it to issue requests for contiguous regions,
with variable ring sizes up to the maximum of 16GB, it seems like significant fragmentation may be achievable. I don't know the practical impact of that but it seems worth avoiding. Are the other users of __vmap (or vmap) for multi-gigabyte regions only either boot-time, infrequent operations (livepatch), or for actions by privileged (ie. somewhat trusted) domains (ioremap), or is it already a frequent operation somewhere else?

Quoted 3 lines

I haven't checked, but I would be quite surprised to find any vmap usage with such size (16GB). Maybe someone more familiar with the mm subsystem can provide some insight here.

And indeed the vmap range reserved in VA space is just 64GB (on x86) at present.

Quoted 15 lines
Quoted 6 lines

Given the context above, and Jason's simplification to the memcpy_to_guest_ring function, plus the imminent merge freeze deadline, and the understanding that this loop and the related data structures supporting it have been tested and are working, would it be acceptable to omit making this contiguous mapping change from this current series?

My opinion would be to just use vmap if it works, because that IMO greatly simplifies the code by being able to have the whole ring mapped at all the time. It would remove the iteration to copy requests, and remove the usage of ring_map_page everywhere. That would be my recommendation code-wise, but as said above someone more familiar with the mm subsystem might have other opinion's about how to deal with accesses to 16GB of guest memory, and indeed your iterative solution might be the best approach.

Quoted 6 lines
Quoted 1 line

No-one can allocate 16GB physically contiguous memory.

Right, my question/comment was whether it would make sense to limit the size of the argos ring to something smaller and then use vmap to map the whole ring in contiguous virtual space in order to ease accesses.

Whether you vmap() the ring in (page sized) pieces or in one blob is, for the purpose of the order of magnitude of VA space consumption, not overly relevant: You can't map more than at most three such gigantic rings anyway with the current VA layout. (In practice mapping individual pages would halve the effectively usable VA space, due to the guard pages inserted between regions.) IOW - the ring size should be bounded at a lower value anyway imo.

Quoted 4 lines

TBH, I'm not sure virtual address space is the only issue if argos allows 16GB rings to be used. 16GB rings will consume a non-trivial amount of memory for the internal argos state tracking structures AFAICT.

Quoted 6 lines

Fully agree. It has taken us ages to eliminate all runtime allocations of order > 0, and it looks like we'd be gaining some back here. I consider this tolerable as long as the feature is experimental, but it would need fixing for it to become fully supported. Christopher - annotating such code with fixme comments right away helps later spotting (and addressing) them.

Sorry for blowing this thread up with the ring size statement based on the incorrect comment (and thanks, Eric, for checking it). I don't think 16MB rings introduce anywhere near the level of concern for internal state, but I'll look at the allocations and see if fixmes are appropriate.

Christopher

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

#85 lore raw

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

In reply to Christopher Clark
Quoted 5 lines · Christopher Clark
xen/common/argo.c +5 -0
+memcpy_to_guest_ring(struct argo_ring_info *ring_info, uint32_t offset,
+                     const void *src, XEN_GUEST_HANDLE(uint8_t) src_hnd,
+                     uint32_t len)
+{
+    unsigned int mfns_index = offset >> PAGE_SHIFT;

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 375

Quoted 10 lines
xen/common/argo.c +10 -0
+    void *dst;
+    int ret;
+    unsigned int src_offset = 0;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    offset &= ~PAGE_MASK;
+
+    if ( (len > XEN_ARGO_MAX_RING_SIZE) || (offset > XEN_ARGO_MAX_RING_SIZE) )
+        return -EFAULT;

With offset < PAGE_SIZE with the previous mask, shouldn't the sanity check be:
if (len + offset > XEN_ARGO_MAX_RING_SIZE)

Signature · 6 lines
Eric Chanudet

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

Re: [PATCH v3 09/15] argo: implement the sendv op; evtchn: expose send_guest_global_virq

Quoted 12 lines · Eric Chanudet
Quoted 10 lines · Christopher Clark
xen/common/argo.c +10 -0
+memcpy_to_guest_ring(struct argo_ring_info *ring_info, uint32_t offset,
+                     const void *src, XEN_GUEST_HANDLE(uint8_t) src_hnd,
+                     uint32_t len)
+{
+    unsigned int mfns_index = offset >> PAGE_SHIFT;
+    void *dst;
+    int ret;
+    unsigned int src_offset = 0;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
Quoted 8 lines

xen/common/argo.c update_tx_ptr(struct argo_ring_info *ring_info, uint32_t tx_ptr) · line 375

Quoted 5 lines
xen/common/argo.c +5 -0
+
+    offset &= ~PAGE_MASK;
+
+    if ( (len > XEN_ARGO_MAX_RING_SIZE) || (offset > XEN_ARGO_MAX_RING_SIZE) )
+        return -EFAULT;

With offset < PAGE_SIZE with the previous mask, shouldn't the sanity check be:
if (len + offset > XEN_ARGO_MAX_RING_SIZE)

Yes, that's correct - thanks.
I'll switch the len and offset arguments to unsigned int while at it.

Christopher

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

#87 lore raw

PATCH v3 10/15 argo: implement the notify op

In reply to Christopher Clark

Queries for data about space availability in registered rings and causes notification to be sent when space has become available.

The hypercall op populates a supplied data structure with information about ring state, and if insufficent space is currently available in a given ring, the hypervisor will record the domain's expressed interest and notify it when it observes that space has become available.

Checks for free space occur when this notify op is invoked, so it may be intentionally invoked with no data structure to populate
(ie. a NULL argument) to trigger such a check and consequent notifications.

Limit the maximum number of notify requests in a single operation to a simple fixed limit of 256.

---
v2 feedback Jan: drop cookie, implement teardown
v2 notify: add flag to indicate ring is shared
v2 argument name for fill_ring_data arg is now currd
v2 self: check ring size vs request and flag error rather than queue signal v2 feedback Jan: drop 'message' from 'argo_message_op'
v2 self: simplify signal_domid, drop unnecessary label + goto v2 self: skip the cookie check in pending_cancel
v2 self: implement npending limit on number of pending entries v1 feedback #16 Jan: sanitize_ring in ringbuf_payload_space
v2 self: inline fill_ring_data_array
v2 self: avoid retesting dst_d for put_domain
v2 self/Jan: remove use of magic verification field and tidy up v1 feedback #16 Jan: remove testing of magic in guest-supplied structure v2 self: s/argo_pending_ent/pending_ent/g
v2 feedback v1#13 Roger: use OS-supplied roundup; drop from public header v1,2 feedback Jan/Roger/Paul: drop errno returning guest access functions v1 feedback Roger, Jan: drop argo prefix on static functions
v2 self: reduce indentation via goto out if arg NULL
v1 feedback #13 Jan: resolve checking of array handle and use of __copy

v1 #5 (#16) feedback Paul: notify op: use currd in do_argo_message_op v1 #5 (#16) feedback Paul: notify op: use currd in argo_notify
v1 #5 (#16) feedback Paul: notify op: use currd in argo_notify_check_pending v1 #5 (#16) feedback Paul: notify op: use currd in argo_fill_ring_data_array v1 #13 (#16) feedback Paul: notify op: do/while: reindent only
v1 #13 (#16) feedback Paul: notify op: do/while: goto
v1 : add compat xlat.lst entries
v1: add definition for copy_field_from_guest_errno
v1 #13 feedback Jan: make 'ring data' comment comply with single-line style v1 feedback #13 Jan: use __copy; so define and use __copy_field_to_guest_errno
v1: #13 feedback Jan: public namespace: prefix with xen
v1: #13 feedback Jan: add blank line after case in do_argo_message_op
v1: self: rename ent id to domain_id
v1: self: ent id-> domain_id
v1: self: drop signal if domain_cookie mismatches
v1. feedback #15 Jan: make loop i unsigned
v1. self: drop unnecessary mb() in argo_notify_check_pending
v1. self: add blank line
v1 #16 feedback Jan: const domain arg to +argo_fill_ring_data
v1. feedback #15 Jan: check unusued hypercall args are zero
v1 feedback #16 Jan: add comment on space available signal policy v1. feedback #16 Jan: move declr, drop braces, lower indent
v1. feedback #18 Jan: meld the resource limits into the main commit v1. feedback #16 Jan: clarify use of magic field
v1. self: use single copy to read notify ring data struct
v1: argo_fill_ring_data: fix dprintk types for port field
v1: self: use %x for printing port as per other print sites
v1. feedback Jan: add comments explaining ring full vs empty
v1. following Jan: fix argo_ringbuf_payload_space calculation for empty ring

 xen/common/argo.c         | 359 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/argo.h |  67 +++++++++
 xen/include/xlat.lst      |   2 +
 3 files changed, 428 insertions(+)
xen/common/argo.c +359 -0
index 4548435..37eb291 100644
@@ -29,6 +29,7 @@
 #include <public/argo.h>
 
 #define MAX_RINGS_PER_DOMAIN            128U
+#define MAX_NOTIFY_COUNT                256U
 #define MAX_PENDING_PER_RING             32U
 
 /* All messages on the ring are padded to a multiple of the slot size. */
@@ -43,6 +44,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_argo_iov_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_page_descr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_register_ring_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_data_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_send_addr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_unregister_ring_t);
 
@@ -231,6 +234,13 @@ static DEFINE_RWLOCK(argo_lock); /* L1 */
 #define argo_dprintk(format, ... ) ((void)0)
 #endif
 
+static struct argo_ring_info *
+ring_find_info(const struct domain *d, const struct argo_ring_id *id);
+
+static struct argo_ring_info *
+ring_find_info_by_match(const struct domain *d, uint32_t port,
+                        domid_t partner_id);
+
 /*
  * This hash function is used to distribute rings within the per-domain
  * hash tables (d->argo->ring_hash and d->argo_send_hash). The hash table
@@ -265,6 +275,17 @@ signal_domain(struct domain *d)
 }
 
 static void
+signal_domid(domid_t domain_id)
+{
+    struct domain *d = get_domain_by_id(domain_id);
+    if ( !d )
+        return;
+
+    signal_domain(d);
+    put_domain(d);
+}
+
+static void
 ring_unmap(struct argo_ring_info *ring_info)
 {
     unsigned int i;
@@ -473,6 +494,62 @@ get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info)
     return 0;
 }
 
+static uint32_t
+ringbuf_payload_space(struct domain *d, struct argo_ring_info *ring_info)
+{
+    xen_argo_ring_t ring;
+    uint32_t len;
+    int32_t ret;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    len = ring_info->len;
+    if ( !len )
+        return 0;
+
+    ret = get_sanitized_ring(&ring, ring_info);
+    if ( ret )
+        return 0;
+
+    argo_dprintk("sanitized ringbuf_payload_space: tx_ptr=%d rx_ptr=%d\n",
+                 ring.tx_ptr, ring.rx_ptr);
+
+    /*
+     * rx_ptr == tx_ptr means that the ring has been emptied, so return
+     * the maximum payload size that can be accepted -- see message size
+     * checking logic in the entry to ringbuf_insert which ensures that
+     * there is always one message slot (of size ROUNDUP_MESSAGE(1)) left
+     * available, preventing a ring from being entirely filled. This ensures
+     * that matching ring indexes always indicate an empty ring and not a
+     * full one.
+     * The subtraction here will not underflow due to minimum size constraints
+     * enforced on ring size elsewhere.
+     */
+    if ( ring.rx_ptr == ring.tx_ptr )
+        return len - sizeof(struct xen_argo_ring_message_header)
+                   - ROUNDUP_MESSAGE(1);
+
+    ret = ring.rx_ptr - ring.tx_ptr;
+    if ( ret < 0 )
+        ret += len;
+
+    /*
+     * The maximum size payload for a message that will be accepted is:
+     * (the available space between the ring indexes)
+     *    minus (space for a message header)
+     *    minus (space for one message slot)
+     * since ringbuf_insert requires that one message slot be left
+     * unfilled, to avoid filling the ring to capacity and confusing a full
+     * ring with an empty one.
+     * Since the ring indexes are sanitized, the value in ret is aligned, so
+     * the simple subtraction here works to return the aligned value needed:
+     */
+    ret -= sizeof(struct xen_argo_ring_message_header);
+    ret -= ROUNDUP_MESSAGE(1);
+
+    return (ret < 0) ? 0 : ret;
+}
+
 /*
  * iov_count returns its count on success via an out variable to avoid
  * potential for a negative return value to be used incorrectly
@@ -812,6 +889,61 @@ pending_remove_all(struct argo_ring_info *ring_info)
     ring_info->npending = 0;
 }
 
+static void
+pending_notify(struct hlist_head *to_notify)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    hlist_for_each_entry_safe(ent, node, next, to_notify, node)
+    {
+        hlist_del(&ent->node);
+        signal_domid(ent->domain_id);
+        xfree(ent);
+    }
+}
+
+static void
+pending_find(const struct domain *d, struct argo_ring_info *ring_info,
+             uint32_t payload_space, struct hlist_head *to_notify)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(rw_is_locked(&d->argo->lock));
+
+    /*
+     * TODO: Current policy here is to signal _all_ of the waiting domains
+     *       interested in sending a message of size less than payload_space.
+     *
+     * This is likely to be suboptimal, since once one of them has added
+     * their message to the ring, there may well be insufficient room
+     * available for any of the others to transmit, meaning that they were
+     * woken in vain, which created extra work just to requeue their wait.
+     *
+     * Retain this simple policy for now since it at least avoids starving a
+     * domain of available space notifications because of a policy that only
+     * notified other domains instead. Improvement may be possible;
+     * investigation required.
+     */
+
+    spin_lock(&ring_info->lock);
+    hlist_for_each_entry_safe(ent, node, next, &ring_info->pending, node)
+    {
+        if ( payload_space >= ent->len )
+        {
+            if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                wildcard_pending_list_remove(ent->domain_id, ent);
+            hlist_del(&ent->node);
+            ring_info->npending--;
+            hlist_add_head(&ent->node, to_notify);
+        }
+    }
+    spin_unlock(&ring_info->lock);
+}
+
 static int
 pending_queue(struct argo_ring_info *ring_info, domid_t src_id,
               unsigned int len)
@@ -874,6 +1006,27 @@ pending_requeue(struct argo_ring_info *ring_info, domid_t src_id,
 }
 
 static void
+pending_cancel(struct argo_ring_info *ring_info, domid_t src_id)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    hlist_for_each_entry_safe(ent, node, next, &ring_info->pending, node)
+    {
+        if ( ent->domain_id == src_id )
+        {
+            if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                wildcard_pending_list_remove(ent->domain_id, ent);
+            hlist_del(&ent->node);
+            xfree(ent);
+            ring_info->npending--;
+        }
+    }
+}
+
+static void
 wildcard_rings_pending_remove(struct domain *d)
 {
     struct hlist_node *node, *next;
@@ -994,6 +1147,92 @@ partner_rings_remove(struct domain *src_d)
 }
 
 static int
+fill_ring_data(const struct domain *currd,
+               XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t) data_ent_hnd)
+{
+    xen_argo_ring_data_ent_t ent;
+    struct domain *dst_d;
+    struct argo_ring_info *ring_info;
+    int ret;
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    ret = __copy_from_guest(&ent, data_ent_hnd, 1) ? -EFAULT : 0;
+    if ( ret )
+        goto out;
+
+    argo_dprintk("fill_ring_data: ent.ring.domain=%u,ent.ring.port=%x\n",
+                 ent.ring.domain_id, ent.ring.port);
+
+    ent.flags = 0;
+
+    dst_d = get_domain_by_id(ent.ring.domain_id);
+    if ( dst_d )
+    {
+        if ( dst_d->argo )
+        {
+            read_lock(&dst_d->argo->lock);
+
+            ring_info = ring_find_info_by_match(dst_d, ent.ring.port,
+                                                currd->domain_id);
+            if ( ring_info )
+            {
+                uint32_t space_avail;
+
+                ent.flags |= XEN_ARGO_RING_DATA_F_EXISTS;
+                ent.max_message_size = ring_info->len -
+                                   sizeof(struct xen_argo_ring_message_header) -
+                                   ROUNDUP_MESSAGE(1);
+
+                if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_SHARED;
+
+                spin_lock(&ring_info->lock);
+
+                space_avail = ringbuf_payload_space(dst_d, ring_info);
+
+                argo_dprintk("fill_ring_data: port=%x space_avail=%u"
+                             " space_wanted=%u\n",
+                             ring_info->id.port, space_avail,
+                             ent.space_required);
+
+                /* Do not queue a notification for an unachievable size */
+                if ( ent.space_required > ent.max_message_size )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_EMSGSIZE;
+                else if ( space_avail >= ent.space_required )
+                {
+                    pending_cancel(ring_info, currd->domain_id);
+                    ent.flags |= XEN_ARGO_RING_DATA_F_SUFFICIENT;
+                }
+                else
+                {
+                    pending_requeue(ring_info, currd->domain_id,
+                                    ent.space_required);
+                    ent.flags |= XEN_ARGO_RING_DATA_F_PENDING;
+                }
+
+                spin_unlock(&ring_info->lock);
+
+                if ( space_avail == ent.max_message_size )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_EMPTY;
+
+            }
+            read_unlock(&dst_d->argo->lock);
+        }
+        put_domain(dst_d);
+    }
+
+    ret = __copy_field_to_guest(data_ent_hnd, &ent, flags) ? -EFAULT : 0;
+    if ( ret )
+        goto out;
+
+    ret = __copy_field_to_guest(data_ent_hnd, &ent, max_message_size) ?
+                -EFAULT : 0;
+ out:
+    return ret;
+}
+
+static int
 find_ring_mfn(struct domain *d, gfn_t gfn, mfn_t *mfn)
 {
     p2m_type_t p2mt;
@@ -1526,6 +1765,111 @@ register_ring(struct domain *currd,
     return ret;
 }
 
+static void
+notify_ring(struct domain *d, struct argo_ring_info *ring_info,
+            struct hlist_head *to_notify)
+{
+    uint32_t space;
+
+    ASSERT(rw_is_locked(&argo_lock));
+    ASSERT(rw_is_locked(&d->argo->lock));
+
+    spin_lock(&ring_info->lock);
+
+    if ( ring_info->len )
+        space = ringbuf_payload_space(d, ring_info);
+    else
+        space = 0;
+
+    spin_unlock(&ring_info->lock);
+
+    if ( space )
+        pending_find(d, ring_info, space, to_notify);
+}
+
+static void
+notify_check_pending(struct domain *currd)
+{
+    unsigned int i;
+    HLIST_HEAD(to_notify);
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    read_lock(&currd->argo->lock);
+
+    for ( i = 0; i < ARGO_HTABLE_SIZE; i++ )
+    {
+        struct hlist_node *node, *next;
+        struct argo_ring_info *ring_info;
+
+        hlist_for_each_entry_safe(ring_info, node, next,
+                                  &currd->argo->ring_hash[i], node)
+        {
+            notify_ring(currd, ring_info, &to_notify);
+        }
+    }
+
+    read_unlock(&currd->argo->lock);
+
+    if ( !hlist_empty(&to_notify) )
+        pending_notify(&to_notify);
+}
+
+static long
+notify(struct domain *currd,
+       XEN_GUEST_HANDLE_PARAM(xen_argo_ring_data_t) ring_data_hnd)
+{
+    XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t) ent_hnd;
+    xen_argo_ring_data_t ring_data;
+    int ret = 0;
+
+    read_lock(&argo_lock);
+
+    if ( !currd->argo )
+    {
+        argo_dprintk("!d->argo, ENODEV\n");
+        ret = -ENODEV;
+        goto out;
+    }
+
+    notify_check_pending(currd);
+
+    if ( guest_handle_is_null(ring_data_hnd) )
+        goto out;
+
+    ret = copy_from_guest(&ring_data, ring_data_hnd, 1) ? -EFAULT : 0;
+    if ( ret )
+        goto out;
+
+    if ( ring_data.nent > MAX_NOTIFY_COUNT )
+    {
+        gprintk(XENLOG_ERR,
+                "argo: notify entry count(%u) exceeds max(%u)\n",
+                ring_data.nent, MAX_NOTIFY_COUNT);
+        ret = -EACCES;
+        goto out;
+    }
+
+    ent_hnd = guest_handle_for_field(ring_data_hnd,
+                                     xen_argo_ring_data_ent_t, data[0]);
+    if ( unlikely(!guest_handle_okay(ent_hnd, ring_data.nent)) )
+    {
+        ret = -EFAULT;
+        goto out;
+    }
+
+    while ( !ret && ring_data.nent-- )
+    {
+        ret = fill_ring_data(currd, ent_hnd);
+        guest_handle_add_offset(ent_hnd, 1);
+    }
+
+ out:
+    read_unlock(&argo_lock);
+
+    return ret;
+}
+
 static long
 sendv(struct domain *src_d, const xen_argo_addr_t *src_addr,
       const xen_argo_addr_t *dst_addr,
@@ -1726,6 +2070,21 @@ do_argo_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) arg1,
         break;
     }
 
+    case XEN_ARGO_OP_notify:
+    {
+        XEN_GUEST_HANDLE_PARAM(xen_argo_ring_data_t) ring_data_hnd =
+                   guest_handle_cast(arg1, xen_argo_ring_data_t);
+
+        if ( unlikely((!guest_handle_is_null(arg2)) || arg3 || arg4) )
+        {
+            rc = -EINVAL;
+            break;
+        }
+
+        rc = notify(currd, ring_data_hnd);
+        break;
+    }
+
     default:
         rc = -EOPNOTSUPP;
         break;
xen/include/public/argo.h +67 -0

77 lines not on this page · raw message

xen/include/xlat.lst +2 -0
index 3723980..e45b60e 100644
@@ -154,3 +154,5 @@
 ?	argo_unregister_ring		argo.h
 ?	argo_iov			argo.h
 ?	argo_send_addr			argo.h
+?	argo_ring_data_ent		argo.h
+?	argo_ring_data			argo.h
Signature · 7 lines
2.7.4


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

Re: [PATCH v3 10/15] argo: implement the notify op

Quoted 6 lines · Christopher Clark

Queries for data about space availability in registered rings and causes notification to be sent when space has become available.

The hypercall op populates a supplied data structure with information about ring state, and if insufficent space is currently available in a given ring,

insufficient

Quoted 89 lines

the hypervisor will record the domain's expressed interest and notify it when it observes that space has become available.

Checks for free space occur when this notify op is invoked, so it may be intentionally invoked with no data structure to populate
(ie. a NULL argument) to trigger such a check and consequent notifications.

Limit the maximum number of notify requests in a single operation to a simple fixed limit of 256.

---
v2 feedback Jan: drop cookie, implement teardown
v2 notify: add flag to indicate ring is shared
v2 argument name for fill_ring_data arg is now currd
v2 self: check ring size vs request and flag error rather than queue signal v2 feedback Jan: drop 'message' from 'argo_message_op'
v2 self: simplify signal_domid, drop unnecessary label + goto v2 self: skip the cookie check in pending_cancel
v2 self: implement npending limit on number of pending entries v1 feedback #16 Jan: sanitize_ring in ringbuf_payload_space
v2 self: inline fill_ring_data_array
v2 self: avoid retesting dst_d for put_domain
v2 self/Jan: remove use of magic verification field and tidy up v1 feedback #16 Jan: remove testing of magic in guest-supplied structure v2 self: s/argo_pending_ent/pending_ent/g
v2 feedback v1#13 Roger: use OS-supplied roundup; drop from public header v1,2 feedback Jan/Roger/Paul: drop errno returning guest access functions v1 feedback Roger, Jan: drop argo prefix on static functions
v2 self: reduce indentation via goto out if arg NULL
v1 feedback #13 Jan: resolve checking of array handle and use of __copy

v1 #5 (#16) feedback Paul: notify op: use currd in do_argo_message_op v1 #5 (#16) feedback Paul: notify op: use currd in argo_notify
v1 #5 (#16) feedback Paul: notify op: use currd in argo_notify_check_pending v1 #5 (#16) feedback Paul: notify op: use currd in argo_fill_ring_data_array v1 #13 (#16) feedback Paul: notify op: do/while: reindent only
v1 #13 (#16) feedback Paul: notify op: do/while: goto
v1 : add compat xlat.lst entries
v1: add definition for copy_field_from_guest_errno
v1 #13 feedback Jan: make 'ring data' comment comply with single-line style v1 feedback #13 Jan: use __copy; so define and use __copy_field_to_guest_errno
v1: #13 feedback Jan: public namespace: prefix with xen
v1: #13 feedback Jan: add blank line after case in do_argo_message_op
v1: self: rename ent id to domain_id
v1: self: ent id-> domain_id
v1: self: drop signal if domain_cookie mismatches
v1. feedback #15 Jan: make loop i unsigned
v1. self: drop unnecessary mb() in argo_notify_check_pending
v1. self: add blank line
v1 #16 feedback Jan: const domain arg to +argo_fill_ring_data
v1. feedback #15 Jan: check unusued hypercall args are zero
v1 feedback #16 Jan: add comment on space available signal policy v1. feedback #16 Jan: move declr, drop braces, lower indent
v1. feedback #18 Jan: meld the resource limits into the main commit v1. feedback #16 Jan: clarify use of magic field
v1. self: use single copy to read notify ring data struct
v1: argo_fill_ring_data: fix dprintk types for port field
v1: self: use %x for printing port as per other print sites
v1. feedback Jan: add comments explaining ring full vs empty
v1. following Jan: fix argo_ringbuf_payload_space calculation for empty ring

 xen/common/argo.c         | 359 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/argo.h |  67 +++++++++
 xen/include/xlat.lst      |   2 +
 3 files changed, 428 insertions(+)
xen/common/argo.c +3 -0
index 4548435..37eb291 100644
@@ -29,6 +29,7 @@
 #include <public/argo.h>
 
 #define MAX_RINGS_PER_DOMAIN            128U
+#define MAX_NOTIFY_COUNT                256U
 #define MAX_PENDING_PER_RING             32U
 
 /* All messages on the ring are padded to a multiple of the slot size. */
@@ -43,6 +44,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_argo_iov_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_page_descr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_register_ring_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_data_t);
+DEFINE_XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_send_addr_t);
 DEFINE_XEN_GUEST_HANDLE(xen_argo_unregister_ring_t);
 

xen/common/argo.c static DEFINE_RWLOCK(argo_lock); /* L1 */ · line 242

Quoted 9 lines
xen/common/argo.c +6 -0
@@ -231,6 +234,13 @@ static DEFINE_RWLOCK(argo_lock); /* L1 */
 #define argo_dprintk(format, ... ) ((void)0)
 #endif
 
+static struct argo_ring_info *
+ring_find_info(const struct domain *d, const struct argo_ring_id *id);
+
+static struct argo_ring_info *
+ring_find_info_by_match(const struct domain *d, uint32_t port,
+                        domid_t partner_id);

Can you place the static functions such that you don't need prototypes for them?

xen/common/argo.c signal_domain(struct domain *d) · line 280

Quoted 11 lines
xen/common/argo.c +4 -0
+
 /*
  * This hash function is used to distribute rings within the per-domain
  * hash tables (d->argo->ring_hash and d->argo_send_hash). The hash table
@@ -265,6 +275,17 @@ signal_domain(struct domain *d)
 }
 
 static void
+signal_domid(domid_t domain_id)
+{
+    struct domain *d = get_domain_by_id(domain_id);

Newline.

Quoted 12 lines
xen/common/argo.c +8 -0
+    if ( !d )
+        return;
+
+    signal_domain(d);
+    put_domain(d);
+}
+
+static void
 ring_unmap(struct argo_ring_info *ring_info)
 {
     unsigned int i;

xen/common/argo.c get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info) · line 502

Quoted 9 lines
xen/common/argo.c +6 -0
@@ -473,6 +494,62 @@ get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info)
     return 0;
 }
 
+static uint32_t
+ringbuf_payload_space(struct domain *d, struct argo_ring_info *ring_info)
+{
+    xen_argo_ring_t ring;
+    uint32_t len;
+    int32_t ret;

You use a signed type to internally store the return value, but the return type from the function itself is unsigned. Is it guaranteed that ret < INT32_MAX always?

Quoted 18 lines
xen/common/argo.c +18 -0
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    len = ring_info->len;
+    if ( !len )
+        return 0;
+
+    ret = get_sanitized_ring(&ring, ring_info);
+    if ( ret )
+        return 0;
+
+    argo_dprintk("sanitized ringbuf_payload_space: tx_ptr=%d rx_ptr=%d\n",
+                 ring.tx_ptr, ring.rx_ptr);
+
+    /*
+     * rx_ptr == tx_ptr means that the ring has been emptied, so return
+     * the maximum payload size that can be accepted -- see message size
+     * checking logic in the entry to ringbuf_insert which ensures that

xen/common/argo.c get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info) · line 530

Quoted 10 lines
xen/common/argo.c +10 -0
+     * there is always one message slot (of size ROUNDUP_MESSAGE(1)) left
+     * available, preventing a ring from being entirely filled. This ensures
+     * that matching ring indexes always indicate an empty ring and not a
+     * full one.
+     * The subtraction here will not underflow due to minimum size constraints
+     * enforced on ring size elsewhere.
+     */
+    if ( ring.rx_ptr == ring.tx_ptr )
+        return len - sizeof(struct xen_argo_ring_message_header)
+                   - ROUNDUP_MESSAGE(1);

Why not do something like:

ret = ring.rx_ptr - ring.tx_ptr;
if ( ret <= 0)

    ret += len;

Instead of this early return?

The only difference when the ring is full is that len should be used instead of the ptr difference.

Quoted 122 lines
xen/common/argo.c +101 -0
+
+    ret = ring.rx_ptr - ring.tx_ptr;
+    if ( ret < 0 )
+        ret += len;
+
+    /*
+     * The maximum size payload for a message that will be accepted is:
+     * (the available space between the ring indexes)
+     *    minus (space for a message header)
+     *    minus (space for one message slot)
+     * since ringbuf_insert requires that one message slot be left
+     * unfilled, to avoid filling the ring to capacity and confusing a full
+     * ring with an empty one.
+     * Since the ring indexes are sanitized, the value in ret is aligned, so
+     * the simple subtraction here works to return the aligned value needed:
+     */
+    ret -= sizeof(struct xen_argo_ring_message_header);
+    ret -= ROUNDUP_MESSAGE(1);
+
+    return (ret < 0) ? 0 : ret;
+}
+
 /*
  * iov_count returns its count on success via an out variable to avoid
  * potential for a negative return value to be used incorrectly
@@ -812,6 +889,61 @@ pending_remove_all(struct argo_ring_info *ring_info)
     ring_info->npending = 0;
 }
 
+static void
+pending_notify(struct hlist_head *to_notify)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    hlist_for_each_entry_safe(ent, node, next, to_notify, node)
+    {
+        hlist_del(&ent->node);
+        signal_domid(ent->domain_id);
+        xfree(ent);
+    }
+}
+
+static void
+pending_find(const struct domain *d, struct argo_ring_info *ring_info,
+             uint32_t payload_space, struct hlist_head *to_notify)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(rw_is_locked(&d->argo->lock));
+
+    /*
+     * TODO: Current policy here is to signal _all_ of the waiting domains
+     *       interested in sending a message of size less than payload_space.
+     *
+     * This is likely to be suboptimal, since once one of them has added
+     * their message to the ring, there may well be insufficient room
+     * available for any of the others to transmit, meaning that they were
+     * woken in vain, which created extra work just to requeue their wait.
+     *
+     * Retain this simple policy for now since it at least avoids starving a
+     * domain of available space notifications because of a policy that only
+     * notified other domains instead. Improvement may be possible;
+     * investigation required.
+     */
+
+    spin_lock(&ring_info->lock);
+    hlist_for_each_entry_safe(ent, node, next, &ring_info->pending, node)
+    {
+        if ( payload_space >= ent->len )
+        {
+            if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                wildcard_pending_list_remove(ent->domain_id, ent);
+            hlist_del(&ent->node);
+            ring_info->npending--;
+            hlist_add_head(&ent->node, to_notify);
+        }
+    }
+    spin_unlock(&ring_info->lock);
+}
+
 static int
 pending_queue(struct argo_ring_info *ring_info, domid_t src_id,
               unsigned int len)
@@ -874,6 +1006,27 @@ pending_requeue(struct argo_ring_info *ring_info, domid_t src_id,
 }
 
 static void
+pending_cancel(struct argo_ring_info *ring_info, domid_t src_id)
+{
+    struct hlist_node *node, *next;
+    struct pending_ent *ent;
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    hlist_for_each_entry_safe(ent, node, next, &ring_info->pending, node)
+    {
+        if ( ent->domain_id == src_id )
+        {
+            if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                wildcard_pending_list_remove(ent->domain_id, ent);
+            hlist_del(&ent->node);
+            xfree(ent);
+            ring_info->npending--;
+        }
+    }
+}
+
+static void
 wildcard_rings_pending_remove(struct domain *d)
 {
     struct hlist_node *node, *next;
@@ -994,6 +1147,92 @@ partner_rings_remove(struct domain *src_d)
 }
 
 static int
+fill_ring_data(const struct domain *currd,
+               XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t) data_ent_hnd)
+{

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1162

Quoted 10 lines
xen/common/argo.c +10 -0
+    xen_argo_ring_data_ent_t ent;
+    struct domain *dst_d;
+    struct argo_ring_info *ring_info;
+    int ret;
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    ret = __copy_from_guest(&ent, data_ent_hnd, 1) ? -EFAULT : 0;
+    if ( ret )
+        goto out;

if ( __copy_from_guest(&ent, data_ent_hnd, 1) )

    return -EFAULT;

And you can get rid of the out label.

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1167

Quoted 5 lines
xen/common/argo.c +5 -0
+
+    argo_dprintk("fill_ring_data: ent.ring.domain=%u,ent.ring.port=%x\n",
+                 ent.ring.domain_id, ent.ring.port);
+
+    ent.flags = 0;

Please memset ent to 0 or initialize it to { }, or else you are leaking hypervisor stack data to the guest in the padding field.

Quoted 107 lines
xen/common/argo.c +100 -0
+
+    dst_d = get_domain_by_id(ent.ring.domain_id);
+    if ( dst_d )
+    {
+        if ( dst_d->argo )
+        {
+            read_lock(&dst_d->argo->lock);
+
+            ring_info = ring_find_info_by_match(dst_d, ent.ring.port,
+                                                currd->domain_id);
+            if ( ring_info )
+            {
+                uint32_t space_avail;
+
+                ent.flags |= XEN_ARGO_RING_DATA_F_EXISTS;
+                ent.max_message_size = ring_info->len -
+                                   sizeof(struct xen_argo_ring_message_header) -
+                                   ROUNDUP_MESSAGE(1);
+
+                if ( ring_info->id.partner_id == XEN_ARGO_DOMID_ANY )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_SHARED;
+
+                spin_lock(&ring_info->lock);
+
+                space_avail = ringbuf_payload_space(dst_d, ring_info);
+
+                argo_dprintk("fill_ring_data: port=%x space_avail=%u"
+                             " space_wanted=%u\n",
+                             ring_info->id.port, space_avail,
+                             ent.space_required);
+
+                /* Do not queue a notification for an unachievable size */
+                if ( ent.space_required > ent.max_message_size )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_EMSGSIZE;
+                else if ( space_avail >= ent.space_required )
+                {
+                    pending_cancel(ring_info, currd->domain_id);
+                    ent.flags |= XEN_ARGO_RING_DATA_F_SUFFICIENT;
+                }
+                else
+                {
+                    pending_requeue(ring_info, currd->domain_id,
+                                    ent.space_required);
+                    ent.flags |= XEN_ARGO_RING_DATA_F_PENDING;
+                }
+
+                spin_unlock(&ring_info->lock);
+
+                if ( space_avail == ent.max_message_size )
+                    ent.flags |= XEN_ARGO_RING_DATA_F_EMPTY;
+
+            }
+            read_unlock(&dst_d->argo->lock);
+        }
+        put_domain(dst_d);
+    }
+
+    ret = __copy_field_to_guest(data_ent_hnd, &ent, flags) ? -EFAULT : 0;
+    if ( ret )
+        goto out;
+
+    ret = __copy_field_to_guest(data_ent_hnd, &ent, max_message_size) ?
+                -EFAULT : 0;
+ out:
+    return ret;
+}
+
+static int
 find_ring_mfn(struct domain *d, gfn_t gfn, mfn_t *mfn)
 {
     p2m_type_t p2mt;
@@ -1526,6 +1765,111 @@ register_ring(struct domain *currd,
     return ret;
 }
 
+static void
+notify_ring(struct domain *d, struct argo_ring_info *ring_info,
+            struct hlist_head *to_notify)
+{
+    uint32_t space;
+
+    ASSERT(rw_is_locked(&argo_lock));
+    ASSERT(rw_is_locked(&d->argo->lock));
+
+    spin_lock(&ring_info->lock);
+
+    if ( ring_info->len )
+        space = ringbuf_payload_space(d, ring_info);
+    else
+        space = 0;
+
+    spin_unlock(&ring_info->lock);
+
+    if ( space )
+        pending_find(d, ring_info, space, to_notify);
+}
+
+static void
+notify_check_pending(struct domain *currd)
+{
+    unsigned int i;
+    HLIST_HEAD(to_notify);
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    read_lock(&currd->argo->lock);
+

xen/common/argo.c register_ring(struct domain *currd, · line 1809

Quoted 10 lines
xen/common/argo.c +10 -0
+    for ( i = 0; i < ARGO_HTABLE_SIZE; i++ )
+    {
+        struct hlist_node *node, *next;
+        struct argo_ring_info *ring_info;
+
+        hlist_for_each_entry_safe(ring_info, node, next,
+                                  &currd->argo->ring_hash[i], node)
+        {
+            notify_ring(currd, ring_info, &to_notify);
+        }

No need for the braces.

Thanks, Roger.

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

#89 lore raw

Re: [PATCH v3 10/15] argo: implement the notify op

Quoted 11 lines · Roger Pau Monné
Quoted 6 lines · Christopher Clark

Queries for data about space availability in registered rings and causes notification to be sent when space has become available.

The hypercall op populates a supplied data structure with information about ring state, and if insufficent space is currently available in a given ring,

insufficient

ack, fixed

Quoted 11 lines
Quoted 11 lines

the hypervisor will record the domain's expressed interest and notify it when it observes that space has become available.

Checks for free space occur when this notify op is invoked, so it may be intentionally invoked with no data structure to populate
(ie. a NULL argument) to trigger such a check and consequent notifications.

Limit the maximum number of notify requests in a single operation to a simple fixed limit of 256.

Quoted 8 lines

xen/common/argo.c static DEFINE_RWLOCK(argo_lock); /* L1 */ · line 242

Quoted 6 lines
xen/common/argo.c +6 -0
+static struct argo_ring_info *
+ring_find_info(const struct domain *d, const struct argo_ring_id *id);
+
+static struct argo_ring_info *
+ring_find_info_by_match(const struct domain *d, uint32_t port,
+                        domid_t partner_id);

Can you place the static functions such that you don't need prototypes for them?

Ack, yes. These have now been pulled to the top of the file and the prototypes removed.

Quoted 7 lines
Quoted 7 lines
xen/common/argo.c +1 -0
+
 /*
  * This hash function is used to distribute rings within the per-domain
  * hash tables (d->argo->ring_hash and d->argo_send_hash). The hash table
@@ -265,6 +275,17 @@ signal_domain(struct domain *d)
 }
 
Quoted 6 lines

xen/common/argo.c signal_domain(struct domain *d) · line 280

Quoted 4 lines
xen/common/argo.c +3 -0
 static void
+signal_domid(domid_t domain_id)
+{
+    struct domain *d = get_domain_by_id(domain_id);

Newline.

ack

Quoted 16 lines
Quoted 16 lines
xen/common/argo.c +8 -0
+    if ( !d )
+        return;
+
+    signal_domain(d);
+    put_domain(d);
+}
+
+static void
 ring_unmap(struct argo_ring_info *ring_info)
 {
     unsigned int i;
@@ -473,6 +494,62 @@ get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info)
     return 0;
 }
 
Quoted 10 lines

xen/common/argo.c get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info) · line 502

Quoted 6 lines
xen/common/argo.c +6 -0
+static uint32_t
+ringbuf_payload_space(struct domain *d, struct argo_ring_info *ring_info)
+{
+    xen_argo_ring_t ring;
+    uint32_t len;
+    int32_t ret;

You use a signed type to internally store the return value, but the return type from the function itself is unsigned. Is it guaranteed that ret < INT32_MAX always?

It is, yes, so I've added a explanatory comment:

/*
* In a sanitized ring, we can rely on:
* (rx_ptr < ring_info->len) &&
* (tx_ptr < ring_info->len) &&
* (ring_info->len <= XEN_ARGO_MAX_RING_SIZE)
*
* and since: XEN_ARGO_MAX_RING_SIZE < INT32_MAX
* therefore right here: ret < INT32_MAX
* and we are safe to return it as a unsigned value from this function.
* The subtractions below cannot increase its value.
*/

Quoted 32 lines
Quoted 19 lines
xen/common/argo.c +18 -0
+
+    ASSERT(spin_is_locked(&ring_info->lock));
+
+    len = ring_info->len;
+    if ( !len )
+        return 0;
+
+    ret = get_sanitized_ring(&ring, ring_info);
+    if ( ret )
+        return 0;
+
+    argo_dprintk("sanitized ringbuf_payload_space: tx_ptr=%d rx_ptr=%d\n",
+                 ring.tx_ptr, ring.rx_ptr);
+
+    /*
+     * rx_ptr == tx_ptr means that the ring has been emptied, so return
+     * the maximum payload size that can be accepted -- see message size
+     * checking logic in the entry to ringbuf_insert which ensures that

xen/common/argo.c get_sanitized_ring(xen_argo_ring_t *ring, struct argo_ring_info *ring_info) · line 530

Quoted 10 lines
xen/common/argo.c +10 -0
+     * there is always one message slot (of size ROUNDUP_MESSAGE(1)) left
+     * available, preventing a ring from being entirely filled. This ensures
+     * that matching ring indexes always indicate an empty ring and not a
+     * full one.
+     * The subtraction here will not underflow due to minimum size constraints
+     * enforced on ring size elsewhere.
+     */
+    if ( ring.rx_ptr == ring.tx_ptr )
+        return len - sizeof(struct xen_argo_ring_message_header)
+                   - ROUNDUP_MESSAGE(1);

Why not do something like:

Quoted 8 lines

ret = ring.rx_ptr - ring.tx_ptr;
if ( ret <= 0)

    ret += len;

Instead of this early return?

The only difference when the ring is full is that len should be used instead of the ptr difference.

That's much nicer -- thanks. Done.

Quoted 17 lines
Quoted 5 lines
xen/common/argo.c +3 -0
 static int
+fill_ring_data(const struct domain *currd,
+               XEN_GUEST_HANDLE(xen_argo_ring_data_ent_t) data_ent_hnd)
+{

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1162

Quoted 10 lines
xen/common/argo.c +10 -0
+    xen_argo_ring_data_ent_t ent;
+    struct domain *dst_d;
+    struct argo_ring_info *ring_info;
+    int ret;
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    ret = __copy_from_guest(&ent, data_ent_hnd, 1) ? -EFAULT : 0;
+    if ( ret )
+        goto out;

if ( __copy_from_guest(&ent, data_ent_hnd, 1) )

Quoted 3 lines
    return -EFAULT;

And you can get rid of the out label.

ack, done.

Quoted 9 lines

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1167

Quoted 6 lines
xen/common/argo.c +5 -0
+
+    argo_dprintk("fill_ring_data: ent.ring.domain=%u,ent.ring.port=%x\n",
+                 ent.ring.domain_id, ent.ring.port);
+
+    ent.flags = 0;

Please memset ent to 0 or initialize it to { }, or else you are leaking hypervisor stack data to the guest in the padding field.

ok - I've added the initializer, thanks.
Was it really leaking stack data though because the struct should have been fully populated, including the padding field, with the __copy_from_guest above?

Quoted 14 lines
Quoted 14 lines
xen/common/argo.c +14 -0
+static void
+notify_check_pending(struct domain *currd)
+{
+    unsigned int i;
+    HLIST_HEAD(to_notify);
+
+    ASSERT(rw_is_locked(&argo_lock));
+
+    read_lock(&currd->argo->lock);
+
+    for ( i = 0; i < ARGO_HTABLE_SIZE; i++ )
+    {
+        struct hlist_node *node, *next;
+        struct argo_ring_info *ring_info;
Quoted 8 lines

xen/common/argo.c register_ring(struct domain *currd, · line 1809

Quoted 6 lines
xen/common/argo.c +6 -0
+
+        hlist_for_each_entry_safe(ring_info, node, next,
+                                  &currd->argo->ring_hash[i], node)
+        {
+            notify_ring(currd, ring_info, &to_notify);
+        }

No need for the braces.

Fixed - thanks.

Christopher

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

#90 lore raw

Re: [PATCH v3 10/15] argo: implement the notify op

Quoted 13 lines · Christopher Clark
Quoted 11 lines · Roger Pau Monné

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1167

Quoted 5 lines · Christopher Clark
xen/common/argo.c +5 -0
+
+    argo_dprintk("fill_ring_data: ent.ring.domain=%u,ent.ring.port=%x\n",
+                 ent.ring.domain_id, ent.ring.port);
+
+    ent.flags = 0;

Please memset ent to 0 or initialize it to { }, or else you are leaking hypervisor stack data to the guest in the padding field.

Quoted 4 lines

ok - I've added the initializer, thanks.
Was it really leaking stack data though because the struct should have been fully populated, including the padding field, with the __copy_from_guest above?

That's my bad, there was no leak here. I somehow missed the copy_from_guest above, even when I made a comment on it. Please leave the code as-is.

Thanks, Roger.

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

#91 lore raw

Re: [PATCH v3 10/15] argo: implement the notify op

Quoted 15 lines · Roger Pau Monné
Quoted 13 lines · Christopher Clark
Quoted 11 lines · Roger Pau Monné

xen/common/argo.c partner_rings_remove(struct domain *src_d) · line 1167

Quoted 5 lines · Christopher Clark
xen/common/argo.c +5 -0
+
+    argo_dprintk("fill_ring_data: ent.ring.domain=%u,ent.ring.port=%x\n",
+                 ent.ring.domain_id, ent.ring.port);
+
+    ent.flags = 0;

Please memset ent to 0 or initialize it to { }, or else you are leaking hypervisor stack data to the guest in the padding field.

Quoted 8 lines
Quoted 4 lines

ok - I've added the initializer, thanks.
Was it really leaking stack data though because the struct should have been fully populated, including the padding field, with the __copy_from_guest above?

That's my bad, there was no leak here. I somehow missed the copy_from_guest above, even when I made a comment on it. Please leave the code as-is.

ok - thanks for the quick response. I've taken the initializer back out.

Christopher

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

#92 lore raw

PATCH v3 11/15 xsm, argo: XSM control for argo register

In reply to Christopher Clark

XSM controls for argo ring registration with two distinct cases, where the ring being registered is:

1) Single source: registering a ring for communication to receive messages from a specified single other domain. Default policy: allow.

2) Any source: registering a ring for communication to receive messages
from any, or all, other domains (ie. wildcard).
Default policy: deny, with runtime policy configuration via bootparam.

The existing argo-mac boot parameter indicates administrator preference for either permissive or strict access control, which will allow or deny registration of any-sender rings.

This commit modifies the signature of core XSM hook functions in order to apply 'const' to arguments, needed in order for 'const' to be accepted in signature of functions that invoke them.

---
v2 feedback #9 Jan: refactor to use argo-mac bootparam at point of introduction v1 feedback Paul: replace use of strncmp with strcmp
v1 feedback #16 Jan: apply const to function signatures
v1 feedback #14 Jan: add blank line before return in parse_argo_mac_param

 xen/common/argo.c                     | 14 ++++++++++----
 xen/include/xsm/dummy.h               | 15 +++++++++++++++
 xen/include/xsm/xsm.h                 | 19 +++++++++++++++++++
 xen/xsm/dummy.c                       |  4 ++++
 xen/xsm/flask/hooks.c                 | 27 ++++++++++++++++++++++++---
 xen/xsm/flask/policy/access_vectors   | 11 +++++++++++
 xen/xsm/flask/policy/security_classes |  1 +
 7 files changed, 84 insertions(+), 7 deletions(-)
xen/common/argo.c +10 -4
index 37eb291..1674f18 100644
@@ -26,6 +26,7 @@
 #include <xen/lib.h>
 #include <xen/nospec.h>
 #include <xen/time.h>
+#include <xsm/xsm.h>
 #include <public/argo.h>
 
 #define MAX_RINGS_PER_DOMAIN            128U
@@ -1582,11 +1583,9 @@ register_ring(struct domain *currd,
 
     if ( reg.partner_id == XEN_ARGO_DOMID_ANY )
     {
-        if ( opt_argo_mac_enforcing )
-        {
-            ret = -EPERM;
+        ret = xsm_argo_register_any_source(currd, opt_argo_mac_enforcing);
+        if ( ret )
             goto out_unlock;
-        }
     }
     else
     {
@@ -1598,6 +1597,13 @@ register_ring(struct domain *currd,
             goto out_unlock;
         }
 
+        ret = xsm_argo_register_single_source(currd, dst_d);
+        if ( ret )
+        {
+            put_domain(dst_d);
+            goto out_unlock;
+        }
+
         if ( !dst_d->argo )
         {
             argo_dprintk("!dst_d->argo, ECONNREFUSED\n");
xen/include/xsm/dummy.h +15 -0
index a29d1ef..55113c3 100644
@@ -720,6 +720,21 @@ static XSM_INLINE int xsm_dm_op(XSM_DEFAULT_ARG struct domain *d)
 
 #endif /* CONFIG_X86 */
 
+#ifdef CONFIG_ARGO
+static XSM_INLINE int xsm_argo_register_single_source(struct domain *d,
+                                                      struct domain *t)
+{
+    return 0;
+}
+
+static XSM_INLINE int xsm_argo_register_any_source(struct domain *d,
+                                                   bool strict)
+{
+    return strict ? -EPERM : 0;
+}
+
+#endif /* CONFIG_ARGO */
+
 #include <public/version.h>
 static XSM_INLINE int xsm_xen_version (XSM_DEFAULT_ARG uint32_t op)
 {
xen/include/xsm/xsm.h +19 -0
index 3b192b5..e775a6d 100644
@@ -181,6 +181,11 @@ struct xsm_operations {
 #endif
     int (*xen_version) (uint32_t cmd);
     int (*domain_resource_map) (struct domain *d);
+#ifdef CONFIG_ARGO
+    int (*argo_register_single_source) (const struct domain *d,
+                                        const struct domain *t);
+    int (*argo_register_any_source) (const struct domain *d);
+#endif
 };
 
 #ifdef CONFIG_XSM
@@ -698,6 +703,20 @@ static inline int xsm_domain_resource_map(xsm_default_t def, struct domain *d)
     return xsm_ops->domain_resource_map(d);
 }
 
+#ifdef CONFIG_ARGO
+static inline xsm_argo_register_single_source(const struct domain *d,
+                                              const struct domain *t)
+{
+    return xsm_ops->argo_register_single_source(d, t);
+}
+
+static inline xsm_argo_register_any_source(const struct domain *d, bool strict)
+{
+    return xsm_ops->argo_register_any_source(d);
+}
+
+#endif /* CONFIG_ARGO */
+
 #endif /* XSM_NO_WRAPPERS */
 
 #ifdef CONFIG_MULTIBOOT
xen/xsm/dummy.c +4 -0
index 5701047..ed236b0 100644
@@ -152,4 +152,8 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)
 #endif
     set_to_dummy_if_null(ops, xen_version);
     set_to_dummy_if_null(ops, domain_resource_map);
+#ifdef CONFIG_ARGO
+    set_to_dummy_if_null(ops, argo_register_single_source);
+    set_to_dummy_if_null(ops, argo_register_any_source);
+#endif
 }
xen/xsm/flask/hooks.c +24 -3
index 96d31aa..fcb7487 100644
@@ -36,13 +36,14 @@
 #include <objsec.h>
 #include <conditional.h>
 
-static u32 domain_sid(struct domain *dom)
+static u32 domain_sid(const struct domain *dom)
 {
     struct domain_security_struct *dsec = dom->ssid;
     return dsec->sid;
 }
 
-static u32 domain_target_sid(struct domain *src, struct domain *dst)
+static u32 domain_target_sid(const struct domain *src,
+                             const struct domain *dst)
 {
     struct domain_security_struct *ssec = src->ssid;
     struct domain_security_struct *dsec = dst->ssid;
@@ -58,7 +59,8 @@ static u32 evtchn_sid(const struct evtchn *chn)
     return chn->ssid.flask_sid;
 }
 
-static int domain_has_perm(struct domain *dom1, struct domain *dom2, 
+static int domain_has_perm(const struct domain *dom1,
+                           const struct domain *dom2,
                            u16 class, u32 perms)
 {
     u32 ssid, tsid;
@@ -1717,6 +1719,21 @@ static int flask_domain_resource_map(struct domain *d)
     return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__RESOURCE_MAP);
 }
 
+#ifdef CONFIG_ARGO
+static int flask_argo_register_single_source(const struct domain *d,
+                                             const struct domain *t)
+{
+    return domain_has_perm(d, t, SECCLASS_ARGO,
+                           ARGO__REGISTER_SINGLE_SOURCE);
+}
+
+static int flask_argo_register_any_source(const struct domain *d)
+{
+    return avc_has_perm(domain_sid(d), SECINITSID_XEN, SECCLASS_ARGO,
+                        ARGO__REGISTER_ANY_SOURCE, NULL);
+}
+#endif
+
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
 int compat_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
 
@@ -1851,6 +1868,10 @@ static struct xsm_operations flask_ops = {
 #endif
     .xen_version = flask_xen_version,
     .domain_resource_map = flask_domain_resource_map,
+#ifdef CONFIG_ARGO
+    .argo_register_single_source = flask_argo_register_single_source,
+    .argo_register_any_source = flask_argo_register_any_source,
+#endif
 };
 
 void __init flask_init(const void *policy_buffer, size_t policy_size)
xen/xsm/flask/policy/access_vectors +11 -0
index 6fecfda..fb95c97 100644
@@ -531,3 +531,14 @@ class version
 # Xen build id
     xen_build_id
 }
+
+# Class argo is used to describe the Argo interdomain communication system.
+class argo
+{
+    # Domain requesting registration of a communication ring
+    # to receive messages from a specific other domain.
+    register_single_source
+    # Domain requesting registration of a communication ring
+    # to receive messages from any other domain.
+    register_any_source
+}
xen/xsm/flask/policy/security_classes +1 -0
index cde4e1a..50ecbab 100644
@@ -19,5 +19,6 @@ class event
 class grant
 class security
 class version
+class argo
 
 # FLASK
Signature · 7 lines
2.7.4


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

PATCH v3 12/15 xsm, argo: XSM control for argo message send operation

In reply to Christopher Clark

Default policy: allow.

---
v2: reordered commit sequence to after sendv implementation v1 feedback Jan #16: apply const to function signatures v1 version was:

 xen/common/argo.c                   | 8 ++++++++
 xen/include/xsm/dummy.h             | 6 ++++++
 xen/include/xsm/xsm.h               | 6 ++++++
 xen/xsm/dummy.c                     | 1 +
 xen/xsm/flask/hooks.c               | 7 +++++++
 xen/xsm/flask/policy/access_vectors | 2 ++
 6 files changed, 30 insertions(+)
xen/common/argo.c +8 -0
index 1674f18..2c0348a 100644
@@ -1922,6 +1922,14 @@ sendv(struct domain *src_d, const xen_argo_addr_t *src_addr,
         goto out_unlock;
     }
 
+    ret = xsm_argo_send(src_d, dst_d);
+    if ( ret )
+    {
+        gprintk(XENLOG_ERR, "argo: XSM REJECTED %i -> %i\n",
+                src_addr->domain_id, dst_addr->domain_id);
+        goto out_unlock;
+    }
+
     read_lock(&dst_d->argo->lock);
 
     ring_info = ring_find_info_by_match(dst_d, dst_addr->port,
xen/include/xsm/dummy.h +6 -0
index 55113c3..05d10b5 100644
@@ -733,6 +733,12 @@ static XSM_INLINE int xsm_argo_register_any_source(struct domain *d,
     return strict ? -EPERM : 0;
 }
 
+static XSM_INLINE int xsm_argo_send(const struct domain *d,
+                                    const struct domain *t)
+{
+    return 0;
+}
+
 #endif /* CONFIG_ARGO */
 
 #include <public/version.h>
xen/include/xsm/xsm.h +6 -0
index e775a6d..4d4a60c 100644
@@ -185,6 +185,7 @@ struct xsm_operations {
     int (*argo_register_single_source) (const struct domain *d,
                                         const struct domain *t);
     int (*argo_register_any_source) (const struct domain *d);
+    int (*argo_send) (const struct domain *d, const struct domain *t);
 #endif
 };
 
@@ -715,6 +716,11 @@ static inline xsm_argo_register_any_source(const struct domain *d, bool strict)
     return xsm_ops->argo_register_any_source(d);
 }
 
+static inline int xsm_argo_send(const struct domain *d, const struct domain *t)
+{
+    return xsm_ops->argo_send(d, t);
+}
+
 #endif /* CONFIG_ARGO */
 
 #endif /* XSM_NO_WRAPPERS */
xen/xsm/dummy.c +1 -0
index ed236b0..ffac774 100644
@@ -155,5 +155,6 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)
 #ifdef CONFIG_ARGO
     set_to_dummy_if_null(ops, argo_register_single_source);
     set_to_dummy_if_null(ops, argo_register_any_source);
+    set_to_dummy_if_null(ops, argo_send);
 #endif
 }
xen/xsm/flask/hooks.c +7 -0
index fcb7487..76c012c 100644
@@ -1732,6 +1732,12 @@ static int flask_argo_register_any_source(const struct domain *d)
     return avc_has_perm(domain_sid(d), SECINITSID_XEN, SECCLASS_ARGO,
                         ARGO__REGISTER_ANY_SOURCE, NULL);
 }
+
+static int flask_argo_send(const struct domain *d, const struct domain *t)
+{
+    return domain_has_perm(d, t, SECCLASS_ARGO, ARGO__SEND);
+}
+
 #endif
 
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
@@ -1871,6 +1877,7 @@ static struct xsm_operations flask_ops = {
 #ifdef CONFIG_ARGO
     .argo_register_single_source = flask_argo_register_single_source,
     .argo_register_any_source = flask_argo_register_any_source,
+    .argo_send = flask_argo_send,
 #endif
 };
 
xen/xsm/flask/policy/access_vectors +2 -0
index fb95c97..f6c5377 100644
@@ -541,4 +541,6 @@ class argo
     # Domain requesting registration of a communication ring
     # to receive messages from any other domain.
     register_any_source
+    # Domain sending a message to another domain.
+    send
 }
Signature · 7 lines
2.7.4


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

PATCH v3 13/15 xsm, argo: XSM control for any access to argo by a domain

In reply to Christopher Clark

Will inhibit initialization of the domain's argo data structure to prevent receiving any messages or notifications and access to any of the argo hypercall operations.

---
v2 self: fix xsm use in soft-reset prior to introduction v1 #5 (#17) feedback Paul: XSM control for any access: use currd v1 #16 feedback Jan: apply const to function signatures

 xen/common/argo.c                   | 6 +++---
 xen/include/xsm/dummy.h             | 5 +++++
 xen/include/xsm/xsm.h               | 6 ++++++
 xen/xsm/dummy.c                     | 1 +
 xen/xsm/flask/hooks.c               | 7 +++++++
 xen/xsm/flask/policy/access_vectors | 3 +++
 6 files changed, 25 insertions(+), 3 deletions(-)
xen/common/argo.c +3 -3
index 2c0348a..31535bd 100644
@@ -1984,7 +1984,7 @@ do_argo_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) arg1,
     argo_dprintk("->do_argo_op(%u,%p,%p,%d,%d)\n", cmd,
                  (void *)arg1.p, (void *)arg2.p, (int) arg3, (int) arg4);
 
-    if ( unlikely(!opt_argo_enabled) )
+    if ( unlikely(!opt_argo_enabled || xsm_argo_enable(currd)) )
     {
         rc = -EOPNOTSUPP;
         return rc;
@@ -2134,7 +2134,7 @@ argo_init(struct domain *d)
 {
     struct argo_domain *argo;
 
-    if ( !opt_argo_enabled )
+    if ( !opt_argo_enabled || xsm_argo_enable(d) )
     {
         argo_dprintk("argo disabled, domid: %d\n", d->domain_id);
         return 0;
@@ -2190,7 +2190,7 @@ argo_soft_reset(struct domain *d)
         partner_rings_remove(d);
         wildcard_rings_pending_remove(d);
 
-        if ( !opt_argo_enabled )
+        if ( !opt_argo_enabled || xsm_argo_enable(d) )
         {
             xfree(d->argo);
             d->argo = NULL;
xen/include/xsm/dummy.h +5 -0
index 05d10b5..91a21c3 100644
@@ -721,6 +721,11 @@ static XSM_INLINE int xsm_dm_op(XSM_DEFAULT_ARG struct domain *d)
 #endif /* CONFIG_X86 */
 
 #ifdef CONFIG_ARGO
+static XSM_INLINE int xsm_argo_enable(struct domain *d)
+{
+    return 0;
+}
+
 static XSM_INLINE int xsm_argo_register_single_source(struct domain *d,
                                                       struct domain *t)
 {
xen/include/xsm/xsm.h +6 -0
index 4d4a60c..e300ebc 100644
@@ -182,6 +182,7 @@ struct xsm_operations {
     int (*xen_version) (uint32_t cmd);
     int (*domain_resource_map) (struct domain *d);
 #ifdef CONFIG_ARGO
+    int (*argo_enable) (const struct domain *d);
     int (*argo_register_single_source) (const struct domain *d,
                                         const struct domain *t);
     int (*argo_register_any_source) (const struct domain *d);
@@ -705,6 +706,11 @@ static inline int xsm_domain_resource_map(xsm_default_t def, struct domain *d)
 }
 
 #ifdef CONFIG_ARGO
+static inline xsm_argo_enable(const struct domain *d)
+{
+    return xsm_ops->argo_enable(d);
+}
+
 static inline xsm_argo_register_single_source(const struct domain *d,
                                               const struct domain *t)
 {
xen/xsm/dummy.c +1 -0
index ffac774..1fe0e74 100644
@@ -153,6 +153,7 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, xen_version);
     set_to_dummy_if_null(ops, domain_resource_map);
 #ifdef CONFIG_ARGO
+    set_to_dummy_if_null(ops, argo_enable);
     set_to_dummy_if_null(ops, argo_register_single_source);
     set_to_dummy_if_null(ops, argo_register_any_source);
     set_to_dummy_if_null(ops, argo_send);
xen/xsm/flask/hooks.c +7 -0
index 76c012c..3d00c74 100644
@@ -1720,6 +1720,12 @@ static int flask_domain_resource_map(struct domain *d)
 }
 
 #ifdef CONFIG_ARGO
+static int flask_argo_enable(const struct domain *d)
+{
+    return avc_has_perm(domain_sid(d), SECINITSID_XEN, SECCLASS_ARGO,
+                        ARGO__ENABLE, NULL);
+}
+
 static int flask_argo_register_single_source(const struct domain *d,
                                              const struct domain *t)
 {
@@ -1875,6 +1881,7 @@ static struct xsm_operations flask_ops = {
     .xen_version = flask_xen_version,
     .domain_resource_map = flask_domain_resource_map,
 #ifdef CONFIG_ARGO
+    .argo_enable = flask_argo_enable,
     .argo_register_single_source = flask_argo_register_single_source,
     .argo_register_any_source = flask_argo_register_any_source,
     .argo_send = flask_argo_send,
xen/xsm/flask/policy/access_vectors +3 -0
index f6c5377..e00448b 100644
@@ -535,6 +535,9 @@ class version
 # Class argo is used to describe the Argo interdomain communication system.
 class argo
 {
+    # Enable initialization of a domain's argo subsystem and
+    # permission to access the argo hypercall operations.
+    enable
     # Domain requesting registration of a communication ring
     # to receive messages from a specific other domain.
     register_single_source
Signature · 7 lines
2.7.4


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

PATCH v3 14/15 xsm, argo: notify: don't describe rings that cannot be sent to

In reply to Christopher Clark
---
 xen/common/argo.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/common/argo.c +11 -0
index 31535bd..0dc064d 100644
@@ -1172,6 +1172,17 @@ fill_ring_data(const struct domain *currd,
     {
         if ( dst_d->argo )
         {
+            /*
+             * Don't supply information about rings that a guest is not
+             * allowed to send to.
+             */
+            ret = xsm_argo_send(currd, dst_d);
+            if ( ret )
+            {
+                put_domain(dst_d);
+                goto out;
+            }
+
             read_lock(&dst_d->argo->lock);
 
             ring_info = ring_find_info_by_match(dst_d, ent.ring.port,
Signature · 7 lines
2.7.4


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

PATCH v3 15/15 argo: validate hypercall arg structures via compat machinery

In reply to Christopher Clark

Argo doesn't use compat hypercall or argument translation but can use some of the infrastructure for validating the hypercall argument structures to ensure that the struct sizes, offsets and compositions don't vary between 32 and 64bit, so add that here in a new dedicated source file for this purpose.

Some of the argo hypercall argument structures contain elements that are hypercall argument structure types themselves, and the standard compat structure validation does not handle this, since the types differ in compat vs. non-compat versions; so for some of the tests the exact-type-match check is replaced with a weaker, but still sufficient, sizeof check.

Then there are additional hypercall argument structures that contain elements that do not have a fixed size (last element, variable length array fields), so we have to then disable that size check too for validating those structures; the coverage of offset of elements is still retained.

---
 xen/common/Makefile      |  2 +-
 xen/common/compat/argo.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 xen/common/compat/argo.c
xen/common/Makefile +1 -1
index 8c65c6f..88b9b2f 100644
@@ -70,7 +70,7 @@ obj-y += xmalloc_tlsf.o
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
 
 
-obj-$(CONFIG_COMPAT) += $(addprefix compat/,domain.o kernel.o memory.o multicall.o xlat.o)
+obj-$(CONFIG_COMPAT) += $(addprefix compat/,argo.o domain.o kernel.o memory.o multicall.o xlat.o)
 
 tmem-y := tmem.o tmem_xen.o tmem_control.o
 tmem-$(CONFIG_COMPAT) += compat/tmem_xen.o
xen/common/compat/argo.c new file +61 -0

61 lines not on this page · raw message

Signature · 7 lines
2.7.4


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

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 07.01.19 at 08:42, <[email protected]> wrote:

Argo doesn't use compat hypercall or argument translation but can use some of the infrastructure for validating the hypercall argument structures to ensure that the struct sizes, offsets and compositions don't vary between 32 and 64bit, so add that here in a new dedicated source file for this purpose.

Some of the argo hypercall argument structures contain elements that are hypercall argument structure types themselves, and the standard compat structure validation does not handle this, since the types differ in compat vs. non-compat versions; so for some of the tests the exact-type-match check is replaced with a weaker, but still sufficient, sizeof check.

"Still sufficient" on what basis? Note that to date we didn't have to make exceptions like this (iirc), so I'm not happy to see some appear.

Quoted 4 lines

Then there are additional hypercall argument structures that contain elements that do not have a fixed size (last element, variable length array fields), so we have to then disable that size check too for validating those structures; the coverage of offset of elements is still retained.

There are prior cases of such as well; I'm not sure though if any were actually in need of checking through these macros. Still I'd like to better understand what it is that doesn't work in that case. Quite possibly there's something that can be fixed in the scripts (or elsewhere).

xen/common/Makefile obj-y += xmalloc_tlsf.o · line 73

Quoted 8 lines
xen/common/Makefile +1 -1
@@ -70,7 +70,7 @@ obj-y += xmalloc_tlsf.o
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
 
 
-obj-$(CONFIG_COMPAT) += $(addprefix compat/,domain.o kernel.o memory.o multicall.o xlat.o)
+obj-$(CONFIG_COMPAT) += $(addprefix compat/,argo.o domain.o kernel.o memory.o multicall.o xlat.o)

While a matter of taste to a certain degree, I'm not convinced introducing a separate file for this is really necessary, especially if some of the overrides to the CHECK_* macros would go away.

Jan

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

#98 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines · On 07.01.19 at 08:42, <[email protected]> wrote:

Argo doesn't use compat hypercall or argument translation but can use some of the infrastructure for validating the hypercall argument structures to ensure that the struct sizes, offsets and compositions don't vary between 32 and 64bit, so add that here in a new dedicated source file for this purpose.

Quoted 7 lines
Quoted 5 lines

Some of the argo hypercall argument structures contain elements that are hypercall argument structure types themselves, and the standard compat structure validation does not handle this, since the types differ in compat vs. non-compat versions; so for some of the tests the exact-type-match check is replaced with a weaker, but still sufficient, sizeof check.

"Still sufficient" on what basis?

I may have overstepped with that assertion, but I meant sufficient for the purposes of checking that the fields within the generated compat structures were the same size and offset, so that copys of data to and from guests that the code performs behave correctly.

Quoted 2 lines

Note that to date we didn't have to make exceptions like this (iirc), so I'm not happy to see some appear.

Yes, that's completely understandable.

Quoted 10 lines
Quoted 4 lines

Then there are additional hypercall argument structures that contain elements that do not have a fixed size (last element, variable length array fields), so we have to then disable that size check too for validating those structures; the coverage of offset of elements is still retained.

There are prior cases of such as well; I'm not sure though if any were actually in need of checking through these macros. Still I'd like to better understand what it is that doesn't work in that case. Quite possibly there's something that can be fixed in the scripts (or elsewhere).

Some details of the problem:

Without the macro overrides in place (ie. using the existing definitions) the build fails on CHECK_argo_send_addr because this struct is defined with types that are themselves translated by the compat processing:

typedef struct xen_argo_send_addr

{
    xen_argo_addr_t src;
    xen_argo_addr_t dst;
} xen_argo_send_addr_t;

compat/argo.c: In function '__checkFstruct_argo_send_addr__src':
xen/include/xen/compat.h:170:18: error: comparison of distinct pointer types lacks a cast [-Werror]

     return &x->f == &c->f; \
                  ^
xen/include/xen/compat.h:176:5: note: in expansion of macro
'CHECK_FIELD_COMMON_'
     CHECK_FIELD_COMMON_(k, CHECK_NAME_(k, n ## __ ## f, F), n, f)
     ^~~~~~~~~~~~~~~~~~~
xen/include/compat/xlat.h:1238:5: note: in expansion of macro 'CHECK_FIELD_'
     CHECK_FIELD_(struct, argo_send_addr, src); \
     ^~~~~~~~~~~~

compat/argo.c:43:1: note: in expansion of macro 'CHECK_argo_send_addr'
CHECK_argo_send_addr;
^~~~~~~~~~~~~~~~~~~~

because xen_argo_addr_t is detected as a different type than compat_argo_addr_t -- when in practice is the same size and has the same fields at the same offsets.

These also fail for the same reason: they also contain types that are compat-converted:
CHECK_argo_ring_data_ent;
CHECK_argo_iov;
CHECK_argo_ring_data;

So the first override substitutes a "sizeof" check for the exact type match, but that doesn't work for CHECK_argo_ring_data, because of the variable-sized array field, so that CHECK has a separate override just for it -- and again, it's only encountering this because the array is of a compat-translated type.

Quoted 7 lines
Quoted 7 lines
xen/common/Makefile +0 -0
@@ -70,7 +70,7 @@ obj-y += xmalloc_tlsf.o
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
 
 
Quoted 6 lines

xen/common/Makefile obj-y += xmalloc_tlsf.o · line 73

Quoted 2 lines
xen/common/Makefile +1 -1
-obj-$(CONFIG_COMPAT) += $(addprefix compat/,domain.o kernel.o memory.o multicall.o xlat.o)
+obj-$(CONFIG_COMPAT) += $(addprefix compat/,argo.o domain.o kernel.o memory.o multicall.o xlat.o)

While a matter of taste to a certain degree, I'm not convinced introducing a separate file for this is really necessary, especially if some of the overrides to the CHECK_* macros would go away.

ack. I wouldn't have moved them out if the overrides weren't in use; but I will merge it into the implementation file if that is preferred.

Christopher

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

#99 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.01.19 at 08:22, <[email protected]> wrote:

Some details of the problem:

Without the macro overrides in place (ie. using the existing definitions) the build fails on CHECK_argo_send_addr because this struct is defined with types that are themselves translated by the compat processing:

But that's a normal situation.

Quoted 18 lines

typedef struct xen_argo_send_addr

{
    xen_argo_addr_t src;
    xen_argo_addr_t dst;
} xen_argo_send_addr_t;

compat/argo.c: In function '__checkFstruct_argo_send_addr__src':
xen/include/xen/compat.h:170:18: error: comparison of distinct pointer types lacks a cast [-Werror]

     return &x->f == &c->f; \
                  ^
xen/include/xen/compat.h:176:5: note: in expansion of macro
'CHECK_FIELD_COMMON_'
     CHECK_FIELD_COMMON_(k, CHECK_NAME_(k, n ## __ ## f, F), n, f)
     ^~~~~~~~~~~~~~~~~~~
xen/include/compat/xlat.h:1238:5: note: in expansion of macro 'CHECK_FIELD_'
     CHECK_FIELD_(struct, argo_send_addr, src); \
     ^~~~~~~~~~~~
Quoted 7 lines

compat/argo.c:43:1: note: in expansion of macro 'CHECK_argo_send_addr'
CHECK_argo_send_addr;
^~~~~~~~~~~~~~~~~~~~

because xen_argo_addr_t is detected as a different type than compat_argo_addr_t -- when in practice is the same size and has the same fields at the same offsets.

Did you perhaps not add entries for the inner structures to xlat.lst?

Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
xen/common/Makefile +0 -0
@@ -70,7 +70,7 @@ obj-y += xmalloc_tlsf.o
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
 
 
Quoted 6 lines

xen/common/Makefile obj-y += xmalloc_tlsf.o · line 73

Quoted 2 lines
xen/common/Makefile +1 -1
-obj-$(CONFIG_COMPAT) += $(addprefix compat/,domain.o kernel.o memory.o multicall.o xlat.o)
+obj-$(CONFIG_COMPAT) += $(addprefix compat/,argo.o domain.o kernel.o memory.o multicall.o xlat.o)

While a matter of taste to a certain degree, I'm not convinced introducing a separate file for this is really necessary, especially if some of the overrides to the CHECK_* macros would go away.

Quoted 2 lines

ack. I wouldn't have moved them out if the overrides weren't in use; but I will merge it into the implementation file if that is preferred.

Well - let's first see whether the overrides are really needed. If so, keeping this in a separate file might indeed be better.

Jan

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

#100 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 10 lines · Jan Beulich
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines · On 17.01.19 at 08:22, <[email protected]> wrote:

Some details of the problem:

Without the macro overrides in place (ie. using the existing definitions) the build fails on CHECK_argo_send_addr because this struct is defined with types that are themselves translated by the compat processing:

But that's a normal situation.

I thought it would be too but I haven't found a direct equivalent to what this header needs. I'll outline the results of my examination below.

Quoted 23 lines
Quoted 23 lines

typedef struct xen_argo_send_addr

{
    xen_argo_addr_t src;
    xen_argo_addr_t dst;
} xen_argo_send_addr_t;

compat/argo.c: In function '__checkFstruct_argo_send_addr__src':
xen/include/xen/compat.h:170:18: error: comparison of distinct pointer types lacks a cast [-Werror]

     return &x->f == &c->f; \
                  ^
xen/include/xen/compat.h:176:5: note: in expansion of macro
'CHECK_FIELD_COMMON_'
     CHECK_FIELD_COMMON_(k, CHECK_NAME_(k, n ## __ ## f, F), n, f)
     ^~~~~~~~~~~~~~~~~~~
xen/include/compat/xlat.h:1238:5: note: in expansion of macro 'CHECK_FIELD_'
     CHECK_FIELD_(struct, argo_send_addr, src); \
     ^~~~~~~~~~~~

compat/argo.c:43:1: note: in expansion of macro 'CHECK_argo_send_addr'
CHECK_argo_send_addr;
^~~~~~~~~~~~~~~~~~~~

Quoted 5 lines
Quoted 3 lines

because xen_argo_addr_t is detected as a different type than compat_argo_addr_t -- when in practice is the same size and has the same fields at the same offsets.

Did you perhaps not add entries for the inner structures to xlat.lst?

No, unfortunately I did.

Here are my findings after exploring the compat machinery, in relation to understanding its processing of the the Argo public header file, and its production of the compat header file and validation macros.

1) Two alternative validation macros are possible as output, depending on how struct fields are declared within structs in the input header.

Struct fields that are themselves structs can be declared two different ways:

* by type (I'll call this "type form"): xen_mystruct_t field;

eg.

    typedef struct xen_argo_send_addr
    {
        xen_argo_addr_t src;
        xen_argo_addr_t dst;
    } xen_argo_send_addr_t;

* by struct name ("struct form"): struct xen_mystruct field;

eg.

    typedef struct xen_argo_send_addr
    {
        struct xen_argo_addr src;
        struct xen_argo_addr dst;
    } xen_argo_send_addr_t;

In the validation macros that are produced for xen_argo_send_addr, the "struct form" contains:

    CHECK_mystruct;

and the "type form" contains instead:

    CHECK_FIELD_(struct, mystruct, field);

These two validation macros do different things; the CHECK_FIELD one is stronger because it tests that the offset, size and type of the field match, whereas the other checks the structure of the inner struct, but not its placement within the outer or matching type in compat vs non-compat.

After reviewing other public headers within Xen and the structs in the xlat.lst list, it looks like some existing structs are passing CHECKs by using the "struct" form, even though the checks are weaker.

2. The "type form" checks cannot work for fields that are themselves compat-translated.

For fields that are struct types that are themselves compat-translated, the CHECK_FIELD fails because it does a typeof check in the macro, which cannot pass since the non-compat type will never equal the compat-type -- the fields really are different types.

So when defining a struct field with a type that will be translated, you have to declare them using the "struct" form, not the "type" form.

A prior example of this is: struct mcinfo_extended
which declares an array field using the struct form, and not its type.

A problem with what the "struct" form generates is that the test that it produces doesn't check the offset of the field within the struct it belongs to. Enabling CHECK_FIELD to work looks preferable as it provides better assurance of correctness.

One way to make CHECK_FIELD work is to override the CHECK_COMMON macro, and disable the typeof check when necessary, as has been presented in the versions of the Argo patch series so far.

3. A challenge with using the "struct" form, following from the result of point 2, occurs when it's a XEN_GUEST_HANDLE field within the struct. It's not obvious how to declare that field using the "struct" form rather than the "type" form.
This affects the argo_iov struct.

4. Macros to perform "struct form" checks cannot be repeated.

When using the "struct" form, it's problem when the struct contains two fields of the same compat-translated type.

eg. consider the "struct form" version of xen_argo_send_addr, which has two fields of struct xen_argo_addr:

    typedef struct xen_argo_send_addr
    {
        struct xen_argo_addr src;
        struct xen_argo_addr dst;
    } xen_argo_send_addr_t;

which then generates this in the compat header:

    #define CHECK_argo_send_addr \
        CHECK_SIZE_(struct, argo_send_addr); \
        CHECK_argo_addr; \
        CHECK_argo_addr

and the second macro invocation of CHECK_argo_addr just breaks, with the build failing due to redefinition of a symbol that is already defined.

A (horrible, unacceptable) workaround that unblocks further discovery is to redefine xen_argo_send_addr to use an array:

typedef struct xen_argo_send_addr

{
    struct xen_argo_addr addrs[2];
} xen_argo_send_addr_t;

which does pass, since it now generates this:

#define CHECK_argo_send_addr \
    CHECK_SIZE_(struct, argo_send_addr); \
    CHECK_argo_addr

and then CHECK_argo_send_addr passes, *but* the fields are no longer named as they should be, which is not OK.

The "no repeated checks" problem also occurs when another separate struct contains a field of a type that has already been checked:
whichever CHECK is performed second will break.

eg.
typedef struct xen_argo_ring_data_ent

{
    struct xen_argo_addr ring;
    uint16_t flags;
    uint16_t pad;
    uint32_t space_required;
    uint32_t max_message_size;
} xen_argo_ring_data_ent_t;

also has a field of type xen_argo_addr, which produces CHECK_argo_addr, which then fails because that was already tested in CHECK_argo_send_addr.

Anyway, hopefully this provides context for evaluating the method of passing the compat tests that has been proposed in the Argo series:
selective override of the CHECK_FIELD_COMMON macro to disable the typeof checks only for validating those structs that require it to be turned off.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

xen/common/Makefile obj-y += xmalloc_tlsf.o · line 73

Quoted 8 lines
xen/common/Makefile +1 -1
@@ -70,7 +70,7 @@ obj-y += xmalloc_tlsf.o
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
 
 
-obj-$(CONFIG_COMPAT) += $(addprefix compat/,domain.o kernel.o memory.o multicall.o xlat.o)
+obj-$(CONFIG_COMPAT) += $(addprefix compat/,argo.o domain.o kernel.o memory.o multicall.o xlat.o)

While a matter of taste to a certain degree, I'm not convinced introducing a separate file for this is really necessary, especially if some of the overrides to the CHECK_* macros would go away.

Quoted 5 lines
Quoted 2 lines

ack. I wouldn't have moved them out if the overrides weren't in use; but I will merge it into the implementation file if that is preferred.

Well - let's first see whether the overrides are really needed. If so, keeping this in a separate file might indeed be better.

ack.

Christopher

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

#101 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 20.01.19 at 22:18, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines · On 17.01.19 at 08:22, <[email protected]> wrote:

Some details of the problem:

Without the macro overrides in place (ie. using the existing definitions) the build fails on CHECK_argo_send_addr because this struct is defined with types that are themselves translated by the compat processing:

Quoted 5 lines
Quoted 1 line

But that's a normal situation.

I thought it would be too but I haven't found a direct equivalent to what this header needs. I'll outline the results of my examination below.

arch-x86/xen-mca.h has

struct mcinfo_global {
    struct mcinfo_common common;
    ...

which results in

#define CHECK_mcinfo_global \
    CHECK_SIZE_(struct, mcinfo_global); \
    CHECK_mcinfo_common; \
    ...

and separately

#define CHECK_mcinfo_common ...

which I would assume ought to similarly work for the Argo structures.

Quoted 5 lines

3. A challenge with using the "struct" form, following from the result of point 2, occurs when it's a XEN_GUEST_HANDLE field within the struct. It's not obvious how to declare that field using the "struct" form rather than the "type" form.
This affects the argo_iov struct.

Structures containing handles are intentionally not covered by the CHECK_* machinery, because handles necessarily need translation due to their different widths in 32- and 64-bit modes on x86.

Quoted 16 lines

4. Macros to perform "struct form" checks cannot be repeated.

When using the "struct" form, it's problem when the struct contains two fields of the same compat-translated type.

eg. consider the "struct form" version of xen_argo_send_addr, which has two fields of struct xen_argo_addr:

    typedef struct xen_argo_send_addr
    {
        struct xen_argo_addr src;
        struct xen_argo_addr dst;
    } xen_argo_send_addr_t;

which then generates this in the compat header:

Quoted 7 lines
    #define CHECK_argo_send_addr \
        CHECK_SIZE_(struct, argo_send_addr); \
        CHECK_argo_addr; \
        CHECK_argo_addr

and the second macro invocation of CHECK_argo_addr just breaks, with the build failing due to redefinition of a symbol that is already defined.

Hmm, this looks like something that indeed wants fixing.

Quoted 14 lines

The "no repeated checks" problem also occurs when another separate struct contains a field of a type that has already been checked:
whichever CHECK is performed second will break.

eg.
typedef struct xen_argo_ring_data_ent

{
    struct xen_argo_addr ring;
    uint16_t flags;
    uint16_t pad;
    uint32_t space_required;
    uint32_t max_message_size;
} xen_argo_ring_data_ent_t;
Quoted 3 lines

also has a field of type xen_argo_addr, which produces CHECK_argo_addr, which then fails because that was already tested in CHECK_argo_send_addr.

Hmm, I think the mcinfo example above contradicts this, because struct mcinfo_common is used by multiple other structures.

Jan

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

#102 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 16 lines
Quoted 16 lines
Quoted 2 lines · On 21.01.19 at 13:03, <[email protected]> wrote:
Quoted 1 line · On 20.01.19 at 22:18, <[email protected]> wrote:

The "no repeated checks" problem also occurs when another separate struct contains a field of a type that has already been checked:
whichever CHECK is performed second will break.

eg.
typedef struct xen_argo_ring_data_ent

{
    struct xen_argo_addr ring;
    uint16_t flags;
    uint16_t pad;
    uint32_t space_required;
    uint32_t max_message_size;
} xen_argo_ring_data_ent_t;
Quoted 6 lines
Quoted 3 lines

also has a field of type xen_argo_addr, which produces CHECK_argo_addr, which then fails because that was already tested in CHECK_argo_send_addr.

Hmm, I think the mcinfo example above contradicts this, because struct mcinfo_common is used by multiple other structures.

Due to

CHECK_mcinfo_common;

# undef xen_mcinfo_common
# undef CHECK_mcinfo_common
# define CHECK_mcinfo_common         struct mcinfo_common

which I think would be easy enough to use in your case as well (until we could perhaps get around and address the underlying issue, albeit it's not really clear to me how that should be done).

Jan

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

#103 lore raw

Re: [PATCH v3 15/15] argo: validate hypercall arg structures via compat machinery

Quoted 12 lines · Jan Beulich
Quoted 11 lines
Quoted 2 lines
Quoted 2 lines · On 20.01.19 at 22:18, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · On 17.01.19 at 08:22, <[email protected]> wrote:

3. A challenge with using the "struct" form, following from the result of point 2, occurs when it's a XEN_GUEST_HANDLE field within the struct. It's not obvious how to declare that field using the "struct" form rather than the "type" form.
This affects the argo_iov struct.

Quoted 4 lines

Structures containing handles are intentionally not covered by the CHECK_* machinery, because handles necessarily need translation due to their different widths in 32- and 64-bit modes on x86.

ack.

Quoted 22 lines
Quoted 22 lines

4. Macros to perform "struct form" checks cannot be repeated.

When using the "struct" form, it's problem when the struct contains two fields of the same compat-translated type.

eg. consider the "struct form" version of xen_argo_send_addr, which has two fields of struct xen_argo_addr:

    typedef struct xen_argo_send_addr
    {
        struct xen_argo_addr src;
        struct xen_argo_addr dst;
    } xen_argo_send_addr_t;

which then generates this in the compat header:

    #define CHECK_argo_send_addr \
        CHECK_SIZE_(struct, argo_send_addr); \
        CHECK_argo_addr; \
        CHECK_argo_addr
Quoted 4 lines
Quoted 2 lines

and the second macro invocation of CHECK_argo_addr just breaks, with the build failing due to redefinition of a symbol that is already defined.

Hmm, this looks like something that indeed wants fixing.

I have a patch to fix that, that it turns out I will not need but can post separately if this is still wanted -- copied here for illustration. (apologies in advance if this gets mail-client mangled here).

xen/tools/get-fields.sh +1 -1
index 45a0e2e..14c6859 100644
@@ -438,7 +438,7 @@ build_check ()
 {
        echo
        echo "#define CHECK_$1 \\"
-       local level=1 fields= kind= id= arrlvl=1 token
+       local level=1 fields= kind= id= arrlvl=1 token suppress_dups=
        for token in $2
        do
                case "$token" in
@@ -470,8 +470,12 @@ build_check ()
                [\,\;])
                        if [ $level = 2 -a -n "$(echo $id | $SED

's,^_pad[[:digit:]]*,,')" ]
then
- check_field $kind $1 $id "$fields"
- test "$token" != ";" || fields= id=
+ if [ "${suppress_dups#*|$kind $1|}" = "${suppress_dups}" ]
+ then
+ check_field $kind $1 $id "$fields"
+ [ -z "$fields" ] ||
suppress_dups="${suppress_dups:-|}$kind $1|"
+ test "$token" != ";" || fields= id=
+ fi

                        fi
                        ;;
                esac
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 17 lines
Quoted 3 lines · On 21.01.19 at 13:03, <[email protected]> wrote:
Quoted 1 line · On 20.01.19 at 22:18, <[email protected]> wrote:

The "no repeated checks" problem also occurs when another separate struct contains a field of a type that has already been checked:
whichever CHECK is performed second will break.

eg.
typedef struct xen_argo_ring_data_ent

{
    struct xen_argo_addr ring;
    uint16_t flags;
    uint16_t pad;
    uint32_t space_required;
    uint32_t max_message_size;
} xen_argo_ring_data_ent_t;
Quoted 6 lines
Quoted 3 lines

also has a field of type xen_argo_addr, which produces CHECK_argo_addr, which then fails because that was already tested in CHECK_argo_send_addr.

Hmm, I think the mcinfo example above contradicts this, because struct mcinfo_common is used by multiple other structures.

Due to

Quoted 8 lines

CHECK_mcinfo_common;

# undef xen_mcinfo_common
# undef CHECK_mcinfo_common
# define CHECK_mcinfo_common         struct mcinfo_common

which I think would be easy enough to use in your case as well (until we could perhaps get around and address the underlying issue, albeit it's not really clear to me how that should be done).

ack, this technique works for the Argo data structures, so I've applied it, dropped the previous macro overrides, moved the checks into the common/argo.c file and dropped common/compat/argo.c, with each check being added at the same time as the structs go in through the series.

Christopher

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