Skip to contentSkip to conversation

xen: Add RING_COPY_RESPONSE()

PATCH In review Marek Marczykowski-Górecki 5 participants 9 messages Apr 30, 2018 – May 3, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: Add RING_COPY_RESPONSE()

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

---
 xen/include/public/io/ring.h | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
xen/include/public/io/ring.h +10 -7
index 30342fc8c0..230fc34cba 100644
@@ -227,22 +227,25 @@ typedef struct __name##_back_ring __name##_back_ring_t
 #define RING_GET_REQUEST(_r, _idx)                                      \
     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
 
+#define RING_GET_RESPONSE(_r, _idx)                                     \
+    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
+
 /*
- * Get a local copy of a request.
+ * Get a local copy of a request/response.
  *
- * Use this in preference to RING_GET_REQUEST() so all processing is
- * done on a local copy that cannot be modified by the other end.
+ * Use this in preference to RING_GET_REQUEST()/RING_GET_RESPONSE() so all
+ * processing is done on a local copy that cannot be modified by the other end.
  *
  * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
  * to be ineffective where _req is a struct which consists of only bitfields.
  */
-#define RING_COPY_REQUEST(_r, _idx, _req) do {				\
+#define RING_COPY_(action, _r, _idx, _req) do {				\
 	/* Use volatile to force the copy into _req. */			\
-	*(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx);	\
+	*(_req) = *(volatile typeof(_req))RING_GET_##action(_r, _idx);	\
 } while (0)
 
-#define RING_GET_RESPONSE(_r, _idx)                                     \
-    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
+#define RING_COPY_REQUEST(_r, _idx, _req) RING_COPY_(REQUEST, _r, _idx, _req)
+#define RING_COPY_RESPONSE(_r, _idx, _req) RING_COPY_(RESPONSE, _r, _idx, _req)
 
 /* Loop termination condition: Would the specified index overflow the ring? */
 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
Signature · 7 lines
2.13.6


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Marek Marczykowski-Górecki

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Quoted 6 lines

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

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

#2 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 4 lines · Marek Marczykowski-Górecki

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

I do not agree with that. Copying still doesn't make all the above safe as nothing prevents the backend from overwriting the response while frontend makes its local copy.

Quoted 43 lines

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

---
  xen/include/public/io/ring.h | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
xen/include/public/io/ring.h +7 -5
index 30342fc8c0..230fc34cba 100644
@@ -227,22 +227,25 @@ typedef struct __name##_back_ring __name##_back_ring_t
  #define RING_GET_REQUEST(_r, _idx)                                      \
      (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
 
+#define RING_GET_RESPONSE(_r, _idx)                                     \
+    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
+
  /*
- * Get a local copy of a request.
+ * Get a local copy of a request/response.
   *
- * Use this in preference to RING_GET_REQUEST() so all processing is
- * done on a local copy that cannot be modified by the other end.
+ * Use this in preference to RING_GET_REQUEST()/RING_GET_RESPONSE() so all
+ * processing is done on a local copy that cannot be modified by the other end.
   *
   * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
   * to be ineffective where _req is a struct which consists of only bitfields.
   */
-#define RING_COPY_REQUEST(_r, _idx, _req) do {				\
+#define RING_COPY_(action, _r, _idx, _req) do {				\
  	/* Use volatile to force the copy into _req. */			\
-	*(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx);	\

xen/include/public/io/ring.h typedef struct __name##_back_ring __name##_back_ring_t · line 251

Quoted 10 lines
xen/include/public/io/ring.h +3 -2
+	*(_req) = *(volatile typeof(_req))RING_GET_##action(_r, _idx);	\
  } while (0)
 
-#define RING_GET_RESPONSE(_r, _idx)                                     \
-    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
+#define RING_COPY_REQUEST(_r, _idx, _req) RING_COPY_(REQUEST, _r, _idx, _req)
+#define RING_COPY_RESPONSE(_r, _idx, _req) RING_COPY_(RESPONSE, _r, _idx, _req)
 
  /* Loop termination condition: Would the specified index overflow the ring? */
  #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \

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

#3 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 02.05.18 at 07:29, <[email protected]> wrote:
Quoted 4 lines · Marek Marczykowski-Górecki

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

I do not agree with that. Copying still doesn't make all the above safe as nothing prevents the backend from overwriting the response while frontend makes its local copy.

But that's not the point here: What the frontend wants is a single, consistent (i.e. not further changing) view of the response, i.e. avoid the multiple reads issue addressed in XSA-155 for backends. Once it has that, it could still apply (sanity) checks to that local copy (just like backends to for requests).

Jan

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

#4 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 02.05.18 at 07:29, <[email protected]> wrote:
Quoted 4 lines · Marek Marczykowski-Górecki

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Quoted 7 lines
Quoted 3 lines

I do not agree with that. Copying still doesn't make all the above safe as nothing prevents the backend from overwriting the response while frontend makes its local copy.

But that's not the point here: What the frontend wants is a single, consistent (i.e. not further changing) view of the response, i.e. avoid the multiple reads issue addressed in XSA-155 for backends. Once it has that, it could still apply (sanity) checks to that local copy (just like backends to for requests).

Ok, but the way it is stated it could make one think we are on the safe side after that copying. I would love to see some sort of the explanation like you gave in the commit message then.

Quoted 2 lines

Jan

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

#5 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 30.04.18 at 23:54, <[email protected]> wrote:

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Quoted 6 lines

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

Please don't forget to Cc maintainers of files you change.

Jan

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

#6 lore raw
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 30.04.18 at 23:54, <[email protected]> wrote:

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Quoted 8 lines
Quoted 6 lines

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

Please don't forget to Cc maintainers of files you change.

According to MAINTAINERS, Konrad is the only one responsible for this file (PUBLIC I/O INTERFACES AND PV DRIVERS DESIGNS). I have plugged get_maintainer.pl to my git send-email, so this is automated. Is anyone missing there?

Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#7 lore raw
Quoted 17 lines · Marek Marczykowski
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 30.04.18 at 23:54, <[email protected]> wrote:

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Please don't forget to Cc maintainers of files you change.

According to MAINTAINERS, Konrad is the only one responsible for this file (PUBLIC I/O INTERFACES AND PV DRIVERS DESIGNS). I have plugged get_maintainer.pl to my git send-email, so this is automated. Is anyone missing there?

Konrad maybe?

Juergen

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

#8 lore raw
Quoted 20 lines · Juergen Gross
Quoted 19 lines · Marek Marczykowski
Quoted 18 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 30.04.18 at 23:54, <[email protected]> wrote:

Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.

Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.

Use a volatile source to prevent the compiler from reordering or omitting the copy.

This is complementary to XSA155.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

Please don't forget to Cc maintainers of files you change.

According to MAINTAINERS, Konrad is the only one responsible for this file (PUBLIC I/O INTERFACES AND PV DRIVERS DESIGNS). I have plugged get_maintainer.pl to my git send-email, so this is automated. Is anyone missing there?

Konrad maybe?

IIRC he has set up mailman such that his CC would be stripped.

Wei.

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