Skip to contentSkip to conversation

xen/HVM: atomically access pointers in bufioreq handling

PATCH In review Jan Beulich 2 participants 21 messages Jun 18, 2015 – Jul 23, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/HVM: atomically access pointers in bufioreq handling

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The Xen side counterpart (which is not a functional prereq to this change, albeit a build one) can be found at e.g.
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg02996.html

xen-hvm.c +18 -8
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {
+            continue;
+        }
+        if (rdptr == wrptr) {
+            break;
+        }
+        buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
         req.size = 1UL << buf_req->size;
         req.count = 1;
         req.addr = buf_req->addr;
@@ -1005,15 +1016,14 @@ static int handle_buffered_iopage(XenIOS
         req.data_is_ptr = 0;
         qw = (req.size == 8);
         if (qw) {
-            buf_req = &state->buffered_io_page->buf_ioreq[
-                (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
+            buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
+                                           IOREQ_BUFFER_SLOT_NUM];
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);
     }
 
     return req.count;
include/hw/xen/xen_common.h +2 -1
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);
 
     if (rc == 0) {
         trace_xen_ioreq_server_create(*ioservid);

xen/HVM: atomically access pointers in bufioreq handling

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The Xen side counterpart (which is not a functional prereq to this change, albeit a build one) can be found at e.g.
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg02996.html

xen-hvm.c +18 -8
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {
+            continue;
+        }
+        if (rdptr == wrptr) {
+            break;
+        }
+        buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
         req.size = 1UL << buf_req->size;
         req.count = 1;
         req.addr = buf_req->addr;
@@ -1005,15 +1016,14 @@ static int handle_buffered_iopage(XenIOS
         req.data_is_ptr = 0;
         qw = (req.size == 8);
         if (qw) {
-            buf_req = &state->buffered_io_page->buf_ioreq[
-                (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
+            buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
+                                           IOREQ_BUFFER_SLOT_NUM];
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);
     }
 
     return req.count;
include/hw/xen/xen_common.h +2 -1
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);
 
     if (rc == 0) {
         trace_xen_ioreq_server_create(*ioservid);
#1 lore raw
Quoted 7 lines · Jan Beulich

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The description is a bit terse: which accesses don't really need to be atomic?

Quoted 21 lines

The Xen side counterpart (which is not a functional prereq to this change, albeit a build one) can be found at e.g.
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg02996.html

xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 10 lines
xen-hvm.c +4 -3
 
     memset(&req, 0x00, sizeof(req));
 
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

Quoted 15 lines
xen-hvm.c +6 -2
+            continue;
+        }
+        if (rdptr == wrptr) {
+            break;
+        }
+        buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
         req.size = 1UL << buf_req->size;
         req.count = 1;
         req.addr = buf_req->addr;
@@ -1005,15 +1016,14 @@ static int handle_buffered_iopage(XenIOS
         req.data_is_ptr = 0;
         qw = (req.size == 8);
         if (qw) {
-            buf_req = &state->buffered_io_page->buf_ioreq[
-                (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 10 lines
xen-hvm.c +3 -2
+            buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
+                                           IOREQ_BUFFER_SLOT_NUM];
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 12 lines
xen-hvm.c +0 -0
     }
 
     return req.count;
include/hw/xen/xen_common.h +2 -1
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Quoted 2 lines
include/hw/xen/xen_common.h +0 -0
     if (rc == 0) {
         trace_xen_ioreq_server_create(*ioservid);
#2 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The description is a bit terse: which accesses don't really need to be atomic?

Quoted 21 lines

The Xen side counterpart (which is not a functional prereq to this change, albeit a build one) can be found at e.g.
http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg02996.html

xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 10 lines
xen-hvm.c +4 -3
 
     memset(&req, 0x00, sizeof(req));
 
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

Quoted 15 lines
xen-hvm.c +6 -2
+            continue;
+        }
+        if (rdptr == wrptr) {
+            break;
+        }
+        buf_req = &buf_page->buf_ioreq[rdptr % IOREQ_BUFFER_SLOT_NUM];
         req.size = 1UL << buf_req->size;
         req.count = 1;
         req.addr = buf_req->addr;
@@ -1005,15 +1016,14 @@ static int handle_buffered_iopage(XenIOS
         req.data_is_ptr = 0;
         qw = (req.size == 8);
         if (qw) {
-            buf_req = &state->buffered_io_page->buf_ioreq[
-                (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 10 lines
xen-hvm.c +3 -2
+            buf_req = &buf_page->buf_ioreq[(rdptr + 1) %
+                                           IOREQ_BUFFER_SLOT_NUM];
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 12 lines
xen-hvm.c +0 -0
     }
 
     return req.count;
include/hw/xen/xen_common.h +2 -1
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Quoted 2 lines
include/hw/xen/xen_common.h +0 -0
     if (rc == 0) {
         trace_xen_ioreq_server_create(*ioservid);
#3 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 21.07.15 at 15:54, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

Quoted 2 lines

The description is a bit terse: which accesses don't really need to be atomic?

Perhaps I should drop this part - I more or less copied the hypervisor side's commit message, and the above really applies to e.g.

if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=
(IOREQ_BUFFER_SLOT_NUM - qw) )

in hypervisor code.

Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 11 lines

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 5 lines
xen-hvm.c +1 -2
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

gcc.pdf, in the section covering them, says "In most cases, these built-in functions are considered a full barrier. [...] Further, instructions are issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation." Details on individual builtins subsequently tell the exceptions from this general rule, but the one used here is not among the exceptions.

Quoted 5 lines
Quoted 5 lines
include/hw/xen/xen_common.h +0 -0
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
Quoted 8 lines

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 4 lines
include/hw/xen/xen_common.h +2 -1
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

Indeed I'm building on it only having done == 0 or != 0 checks.

Quoted 2 lines

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Which is what I aimed at avoiding.

Jan

#4 lore raw
In reply to Stefano Stabellini
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 21.07.15 at 15:54, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

Quoted 2 lines

The description is a bit terse: which accesses don't really need to be atomic?

Perhaps I should drop this part - I more or less copied the hypervisor side's commit message, and the above really applies to e.g.

if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=
(IOREQ_BUFFER_SLOT_NUM - qw) )

in hypervisor code.

Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 11 lines

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 5 lines
xen-hvm.c +1 -2
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

gcc.pdf, in the section covering them, says "In most cases, these built-in functions are considered a full barrier. [...] Further, instructions are issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation." Details on individual builtins subsequently tell the exceptions from this general rule, but the one used here is not among the exceptions.

Quoted 5 lines
Quoted 5 lines
include/hw/xen/xen_common.h +0 -0
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
Quoted 8 lines

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 4 lines
include/hw/xen/xen_common.h +2 -1
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

Indeed I'm building on it only having done == 0 or != 0 checks.

Quoted 2 lines

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Which is what I aimed at avoiding.

Jan

#5 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 7 lines

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The description is a bit terse: which accesses don't really need to be atomic?

Quoted 7 lines

Perhaps I should drop this part - I more or less copied the hypervisor side's commit message, and the above really applies to e.g.

if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=
(IOREQ_BUFFER_SLOT_NUM - qw) )

in hypervisor code.

Yes, please remove it as it is confusing.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 9 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway? In any case, if you follow my suggestion below, it won't matter.

Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

it would work, wouldn't it?

Quoted 12 lines
Quoted 11 lines

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 5 lines
xen-hvm.c +1 -2
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

Quoted 7 lines

gcc.pdf, in the section covering them, says "In most cases, these built-in functions are considered a full barrier. [...] Further, instructions are issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation." Details on individual builtins subsequently tell the exceptions from this general rule, but the one used here is not among the exceptions.

Good, thanks for looking it up

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
include/hw/xen/xen_common.h +0 -0
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
Quoted 8 lines

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 4 lines
include/hw/xen/xen_common.h +2 -1
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

Quoted 6 lines

Indeed I'm building on it only having done == 0 or != 0 checks.

Quoted 2 lines

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Which is what I aimed at avoiding.

OK

#6 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 7 lines

The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. The hypervisor side gets I/O req server creation extended so we can indicate that we're using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing it to atomically canonicalize both pointers when both have gone through at least one cycle.

The description is a bit terse: which accesses don't really need to be atomic?

Quoted 7 lines

Perhaps I should drop this part - I more or less copied the hypervisor side's commit message, and the above really applies to e.g.

if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=
(IOREQ_BUFFER_SLOT_NUM - qw) )

in hypervisor code.

Yes, please remove it as it is confusing.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 9 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway? In any case, if you follow my suggestion below, it won't matter.

Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

it would work, wouldn't it?

Quoted 12 lines
Quoted 11 lines

xen-hvm.c static int handle_buffered_iopage(XenIOS · line 1026

Quoted 5 lines
xen-hvm.c +1 -2
         handle_ioreq(state, &req);
 
-        xen_mb();
-        state->buffered_io_page->read_pointer += qw ? 2 : 1;
+        atomic_add(&buf_page->read_pointer, qw + 1);

I couldn't get specific info on the type of barrier implemented by __sync_fetch_and_add, so I cannot tell for sure whether removing xen_mb() is appropriate. Do you have a link? I suspect that given the strong guarantees of the x86 architecture we'll be fine. I would be less confident if this code was used on other archs.

Quoted 7 lines

gcc.pdf, in the section covering them, says "In most cases, these built-in functions are considered a full barrier. [...] Further, instructions are issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation." Details on individual builtins subsequently tell the exceptions from this general rule, but the one used here is not among the exceptions.

Good, thanks for looking it up

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
include/hw/xen/xen_common.h +0 -0
@@ -370,7 +370,8 @@ static inline void xen_unmap_pcidev(XenX
 static inline int xen_create_ioreq_server(XenXC xc, domid_t dom,
                                           ioservid_t *ioservid)
Quoted 8 lines

include/hw/xen/xen_common.h static inline void xen_unmap_pcidev(XenX · line 374

Quoted 4 lines
include/hw/xen/xen_common.h +2 -1
 {
-    int rc = xc_hvm_create_ioreq_server(xc, dom, 1, ioservid);
+    int rc = xc_hvm_create_ioreq_server(xc, dom, HVM_IOREQSRV_BUFIOREQ_ATOMIC,
+                                        ioservid);

I am concerned that passing 2 instead of 1 could break older hypervisors. However handle_bufioreq was never defined as a true boolean, so maybe it is OK?

Quoted 6 lines

Indeed I'm building on it only having done == 0 or != 0 checks.

Quoted 2 lines

The alternative would be to create a xen_xc_hvm_create_ioreq_server versioned wrapper in include/hw/xen/xen_common.h.

Which is what I aimed at avoiding.

OK

#7 lore raw
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Quoted 17 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);
Quoted 3 lines

}

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Jan

#8 lore raw
In reply to Stefano Stabellini
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Quoted 17 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);
Quoted 3 lines

}

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Jan

#9 lore raw
Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

OK

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 8 lines
Quoted 3 lines

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Damn!

Quoted 19 lines
Quoted 19 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

Quoted 6 lines
Quoted 1 line

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Actually I think it is conceptually easier to understand, but the current implementation of atomic_read not working with uint64_t on x86_32 is a real bummer. In that case I am OK with the lock free loop too. Thanks for the explanation.

I'll queue this change up for the next QEMU release cycle.

#10 lore raw
In reply to Jan Beulich
Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

OK

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 8 lines
Quoted 3 lines

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Damn!

Quoted 19 lines
Quoted 19 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

Quoted 6 lines
Quoted 1 line

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Actually I think it is conceptually easier to understand, but the current implementation of atomic_read not working with uint64_t on x86_32 is a real bummer. In that case I am OK with the lock free loop too. Thanks for the explanation.

I'll queue this change up for the next QEMU release cycle.

#11 lore raw
Quoted 102 lines · Stefano Stabellini
Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

OK

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 8 lines
Quoted 3 lines

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Damn!

Quoted 19 lines
Quoted 19 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

Quoted 6 lines
Quoted 1 line

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Quoted 6 lines

Actually I think it is conceptually easier to understand, but the current implementation of atomic_read not working with uint64_t on x86_32 is a real bummer. In that case I am OK with the lock free loop too. Thanks for the explanation.

I'll queue this change up for the next QEMU release cycle.

I forgot about the commit message change. Please resend, then, provided that everything is OK, I'll queue it up.

#12 lore raw
In reply to Stefano Stabellini
Quoted 102 lines · Stefano Stabellini
Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 16:50, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 18 lines
Quoted 18 lines
xen-hvm.c +2 -1
@@ -981,19 +981,30 @@ static void handle_ioreq(XenIOState *sta
 
 static int handle_buffered_iopage(XenIOState *state)
 {
+    buffered_iopage_t *buf_page = state->buffered_io_page;
     buf_ioreq_t *buf_req = NULL;
     ioreq_t req;
     int qw;
 
-    if (!state->buffered_io_page) {
+    if (!buf_page) {
         return 0;
     }
 
     memset(&req, 0x00, sizeof(req));
 
Quoted 9 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 998

Quoted 7 lines
xen-hvm.c +4 -3
-    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
-        buf_req = &state->buffered_io_page->buf_ioreq[
-            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
+    for (;;) {
+        uint32_t rdptr = buf_page->read_pointer, wrptr;
+
+        xen_rmb();

We don't need this barrier.

Quoted 4 lines

How would we not? We need to make sure we read in this order read_pointer, write_pointer, and read_pointer again (in the comparison). Only that way we can be certain to hold a matching pair in hands at the end.

See below

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void handle_ioreq(XenIOState *sta · line 1001

Quoted 3 lines
xen-hvm.c +3 -0
+        wrptr = buf_page->write_pointer;
+        xen_rmb();
+        if (rdptr != buf_page->read_pointer) {

I think you have to use atomic_read to be sure that the second read to buf_page->read_pointer is up to date and not optimized away.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

No, suppressing such an optimization is an intended (side) effect of the barriers used.

I understand what you are saying but I am not sure if your assumption is correct. Can the compiler optimize the second read anyway?

No, it can't, due to the barrier.

OK

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

But if I think that it would be best to simply use atomic_read to read both pointers at once using uint64_t as type, so you are sure to get a consistent view and there is no need for this check.

But I'm specifically trying to avoid e.g. a locked cmpxchg8b here on ix86.

Quoted 8 lines
Quoted 3 lines

OK, but we don't need cmpxchg8b, just:

#define atomic_read(ptr) (*(__typeof__(*ptr) volatile*) (ptr))

This only gives the impression of being atomic when the type is wider than a machine word. There's no ix86 (i.e. 32-bit) instruction other than LOCK CMPXCHG8B (and possibly MMX/SSE/AVX ones) allowing to atomically read a 64-bit quantity.

Damn!

Quoted 19 lines
Quoted 19 lines

something like:

 for (;;) {
     uint64_t ptrs;
     uint32_t rdptr, wrptr;
 
     ptrs = atomic_read((uint64_t*)&state->buffered_io_page->read_pointer);
     rdptr = (uint32_t)ptrs;
     wrptr = *(((uint32_t*)&ptrs) + 1);
 
     if (rdptr == wrptr) {
         continue;
     }
 
     [work]
 
     atomic_add(&buf_page->read_pointer, qw + 1);

}

Quoted 6 lines
Quoted 1 line

it would work, wouldn't it?

Looks like so, but the amount of casts alone makes me wish for no-one to consider this (but I agree that the casts could be taken care of). Still I think (as btw done elsewhere) the lock free access is preferable.

Quoted 6 lines

Actually I think it is conceptually easier to understand, but the current implementation of atomic_read not working with uint64_t on x86_32 is a real bummer. In that case I am OK with the lock free loop too. Thanks for the explanation.

I'll queue this change up for the next QEMU release cycle.

I forgot about the commit message change. Please resend, then, provided that everything is OK, I'll queue it up.

#13 lore raw
In reply to Stefano Stabellini
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Jan

#14 lore raw
In reply to Stefano Stabellini
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Jan

#15 lore raw
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

#16 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

#17 lore raw
Quoted 11 lines · Stefano Stabellini
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

Never mind, I have seen it is already in.

#18 lore raw
In reply to Stefano Stabellini
Quoted 11 lines · Stefano Stabellini
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

Never mind, I have seen it is already in.

#19 lore raw
In reply to Stefano Stabellini
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 23.07.15 at 12:04, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Quoted 7 lines
Quoted 4 lines

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

As said in the updated description in v2, the hypervisor side went in already some time ago.

Jan

#20 lore raw
In reply to Stefano Stabellini
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 23.07.15 at 12:04, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.07.15 at 19:24, <[email protected]> wrote:

I'll queue this change up for the next QEMU release cycle.

Quoted 7 lines
Quoted 4 lines

Thanks - v2 (with the adjusted description) just sent.

It would however be nice for our variant in 4.6 to also gain this, perhaps independent of upstream's schedule.

Is the hypervisor side going to go in 4.6? Do we need a freeze exception or do we consider this a bug fix?

As said in the updated description in v2, the hypervisor side went in already some time ago.

Jan